We’re preparing your current view and syncing the latest data.
Given an array of integers, find the first element in the array such that the frequency of that element is unique in the array. The frequency of an element is defined as the number of times it appears in the array. Return the first such element encountered when traversing the array from left to right. If no such element exists, return -1.
An integer array nums.
An integer representing the first element with a unique frequency or -1 if no such element exists.
1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
Example 1
Input
[1, 2, 2, 3, 3, 3]
Output
1
Explanation
Frequency of elements are {1:1, 2:2, 3:3}. Frequencies 2 and 3 appear more than once, but frequency 1 is unique for element 1.
Example 2
Input
[2, 2, 3, 3]
Output
-1
Explanation
Both elements 2 and 3 have frequency 2. No unique frequency.