Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Ordered Structures
First Element With Unique Frequency

Find the first element in an array whose frequency is exactly one.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

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 nums whose frequency is 1, or -1 if none exists.

Constraints

  • 1 <= nums.length
  • Values may be repeated.
  • Use only safe assumptions typical for an interview setting; exact platform constraints are unspecified.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.