First Unique Elements in Array
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.
Implement the function findFirstUniqueElements that takes an array of integers nums as input. Your task is to:
nums that appear exactly once.nums array.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.nums is an empty array, the function should return an empty array [].nums appear more than once, the function should return an empty array [].nums can contain negative integers.nums are guaranteed to be integers.Examples
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].
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.
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
1 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9nums.Loading editor...