Find the first element in an array whose frequency is exactly one.
Problem
Given an array of integers, return the first element whose value appears exactly once in the array.
If no such element exists, return -1.
The answer should be the element that appears earliest in the original array among all values with frequency one.
Notes
- You must consider the original order of the array, not sorted order.
- If multiple elements occur once, choose the leftmost one.
Input Format
- An integer array
nums.
Output Format
- Return the first value in
numswhose frequency is1, or-1if none exists.
Constraints
1 <= nums.length- Values may be repeated.
- Use only safe assumptions typical for an interview setting; exact platform constraints are unspecified.
Example 1
Input
nums = [9, 4, 9, 6, 7, 4]
Output
6
Explanation
Frequencies are: 9 -> 2, 4 -> 2, 6 -> 1, 7 -> 1. The first value with frequency one is 6.
Example 2
Input
nums = [1, 1, 2, 2, 3, 3]
Output
-1
Explanation
Every value appears more than once, so there is no unique element.
Show 1 more example
Example 3
Input
nums = [5, 8, 5, 9, 8]
Output
9
Explanation
Only 9 appears exactly once, so it is the answer.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.