Frontend School

First Unique Elements in Array

DSA
easy

First Unique Elements in Array

Easy
Arrays
Hashing

Overview

In many data processing scenarios, it's crucial to identify and extract unique pieces of information from a larger dataset. For instance, in log analysis, you might want to find events that occurred only once, indicating a singular, potentially important, system state change or user action. This problem challenges your ability to efficiently filter an array of integers to find elements that appear exactly once, while preserving their original order of appearance.

What you need to do / Task

Implement the function findFirstUniqueElements that takes an array of integers nums as input. Your task is to:

  1. Identify all elements within nums that appear exactly once.
  2. Return these unique elements in a new array.
  3. The order of elements in the output array must match the order of their first appearance in the original nums array.

Input & output

Input:

  • nums: number[] - An array of integers.

Output:

  • number[] - A new array containing only the elements from nums that appear exactly once, ordered by their first appearance.

Clarifications

  • Empty Input: If nums is an empty array, the function should return an empty array [].
  • All Duplicates: If all elements in nums appear more than once, the function should return an empty array [].
  • Negative Numbers: nums can contain negative integers.
  • Data Type: All elements in nums are guaranteed to be integers.

Examples

1

Example 1

Input:

nums = [1, 2, 2, 3, 1, 4, 5, 4]

Output:

[3,5]

Note:

Elements 1, 2, and 4 appear multiple times. Elements 3 and 5 appear exactly once. 3 first appears at index 3, and 5 first appears at index 6. So the output is [3, 5].

2

Example 2

Input:

nums = [10, 20, 30, 40]

Output:

[10,20,30,40]

Note:

All elements appear exactly once, so they are all included in the output in their original order.

3

Example 3

Input:

nums = [7, 7, 7, 7]

Output:

[]

Note:

Every element (7) appears four times, which is more than once. Therefore, there are no unique elements, and an empty array is returned.

Constraints

Constraints & assumptions

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The solution should aim for optimal time complexity, ideally O(N), where N is the length of nums.
  • Space complexity should be considered, with O(N) auxiliary space generally acceptable.

Out of scope

  • Input validation for non-integer or non-array inputs.
  • Handling extremely large numbers that exceed standard integer type limits.

What interviewers evaluate

  • Correctness: Does the solution accurately identify all unique elements and preserve their first appearance order?
  • Efficiency: Is the chosen algorithm performant enough for the given constraints, particularly regarding time and space complexity?
  • Code Quality: Is the code clean, readable, well-structured, and does it handle common edge cases gracefully?
  • Problem-Solving: Demonstrates a clear thought process in breaking down the problem and selecting appropriate data structures.

Loading editor...