First Element with Unique Frequency

You are given an integer array nums.

Your task is to find the first element in the array (scanning from left to right) that has a unique frequency. An element's frequency is considered unique if no other distinct integer in the array appears the exact same number of times.

Return that element. If no such element exists, return -1.

Example 1:

Input: nums = [1, 2, 3, 4]

Output: -1

Explanation:

The elements 1, 2, 3, and 4 all appear exactly 1 time. Since they all share the same frequency, no frequency is unique.

Example 2:

Input: nums = [7, 8, 8, 9, 9, 9]

Output: 7

Explanation:

7 appears 1 time, 8 appears 2 times, and 9 appears 3 times. All frequencies (1, 2, and 3) are unique. The first element in the array from left to right is 7.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [5,5,5,2,2,3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int firstUniqueFrequency(vector<int>& nums) {
// Write your code here
}
};
Test Case

Input:

Nums