problemeasyalgorithmsleetcode-1133leetcode 1133leetcode1133

Largest Unique Number

EasyUpdated: Aug 2, 2025
Practice on:

Problem

Given an integer array nums, return the largest integer that only occurs once. If no integer occurs once, return -1.

Examples

Example 1:

Input: nums = [5,7,3,9,4,9,8,3,1]
Output: 8
Explanation: The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it is the answer.

Example 2:

Input: nums = [9,9,8,8]
Output: -1
Explanation: There is no number that occurs only once.

Constraints:

  • 1 <= nums.length <= 2000
  • 0 <= nums[i] <= 1000

Solution

Method 1 – Hash Map Counting and Max Selection

Intuition

To find the largest unique number, count the frequency of each number, then select the largest number that appears exactly once.

Approach

  1. Count the frequency of each number using a hash map.
  2. Iterate through the keys and select the largest number with frequency 1.
  3. Return the result, or -1 if no such number exists.

Code

C++
class Solution {
public:
    int largestUniqueNumber(vector<int>& nums) {
        unordered_map<int, int> cnt;
        for (int n : nums) cnt[n]++;
        int ans = -1;
        for (auto& [n, c] : cnt) if (c == 1) ans = max(ans, n);
        return ans;
    }
};
Go
func largestUniqueNumber(nums []int) int {
    cnt := map[int]int{}
    for _, n := range nums { cnt[n]++ }
    ans := -1
    for n, c := range cnt { if c == 1 && n > ans { ans = n } }
    return ans
}
Java
class Solution {
    public int largestUniqueNumber(int[] nums) {
        Map<Integer, Integer> cnt = new HashMap<>();
        for (int n : nums) cnt.put(n, cnt.getOrDefault(n, 0) + 1);
        int ans = -1;
        for (int n : cnt.keySet()) if (cnt.get(n) == 1 && n > ans) ans = n;
        return ans;
    }
}
Kotlin
class Solution {
    fun largestUniqueNumber(nums: IntArray): Int {
        val cnt = mutableMapOf<Int, Int>()
        for (n in nums) cnt[n] = cnt.getOrDefault(n, 0) + 1
        var ans = -1
        for ((n, c) in cnt) if (c == 1 && n > ans) ans = n
        return ans
    }
}
Python
class Solution:
    def largestUniqueNumber(self, nums: list[int]) -> int:
        from collections import Counter
        cnt = Counter(nums)
        ans = -1
        for n, c in cnt.items():
            if c == 1 and n > ans:
                ans = n
        return ans
Rust
use std::collections::HashMap;
impl Solution {
    pub fn largest_unique_number(nums: Vec<i32>) -> i32 {
        let mut cnt = HashMap::new();
        for n in nums { *cnt.entry(n).or_insert(0) += 1; }
        let mut ans = -1;
        for (&n, &c) in &cnt { if c == 1 && n > ans { ans = n; } }
        ans
    }
}
TypeScript
class Solution {
    largestUniqueNumber(nums: number[]): number {
        const cnt = new Map<number, number>()
        for (const n of nums) cnt.set(n, (cnt.get(n) ?? 0) + 1)
        let ans = -1
        for (const [n, c] of cnt.entries()) if (c === 1 && n > ans) ans = n
        return ans
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of nums. Counting and selection are linear.
  • 🧺 Space complexity: O(n), for the hash map.

Comments