Largest Unique Number

Updated on 06 June, 2025
Largest Unique Number header image

Problem Statement

The objective is to identify the largest integer from an array called nums that appears only once. If every integer in the array appears more than once, the function should return -1. This involves checking both the occurrence count and value of elements in the array to arrive at the solution.

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

Approach and Intuition

  • The operation needs to differentiate between distinct numbers and their counts. Given an integer array nums, our job is to scan through this list, tally the occurrences of each number, and then determine which of these uniquely occurring numbers is the largest.
  1. Utilize a dictionary to count occurrences of each number in the array. This helps in efficiently identifying numbers that occur only once as well as their frequency.

  2. Traverse the list nums and populate the dictionary where keys are numbers from the array and values are their respective counts.

  3. After building the count dictionary, iterate through the dictionary to find the maximum number that has a count of one.

  4. Remember to handle the case where no number appears exactly once. If such a situation occurs, the function should return -1.

  • The given constraints ensure the function handles:

    • a minimum array length of 1 and a maximum of 2000,
    • numbers ranging from 0 to 1000.
  • These constraints mean our approach needs to be efficient but offer enough space and time to handle the upper limits comfortably without resorting to more complex data structures or algorithms. The linear scan for counting and then another for finding the maximum unique number is manageable within these limits.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int maxDistinctNum(vector<int>& elements) {
        unordered_map<int, int> countMap;

        for (int el : elements) {
            countMap[el]++;
        }

        int highestUnique = -1;

        for (auto& kvp : countMap) {
            if (kvp.second == 1 && kvp.first > highestUnique) {
                highestUnique = kvp.first;
            }
        }

        return highestUnique;
    }
};

The given C++ code provides a solution to the problem of finding the largest unique number from a list of integers. The function maxDistinctNum takes a vector of integers as input and determines this number using the following approach:

  • Use unordered_map from the C++ Standard Library to count occurrences of each element in the vector. This data structure provides efficient average time complexity for lookup, insertion, and deletion operations.

  • Iterate over each element in the input vector and use this loop to populate the countMap with the frequency of each number.

  • Initialize highestUnique to -1, which will store the value of the largest integer that appears exactly once in the vector.

  • Traverse the countMap using a for loop. This evaluation checks each key-value pair; if a number (kvp.first) appears exactly once (kvp.second == 1) and is greater than the current highestUnique, update highestUnique.

  • Finally, return the value of highestUnique, which represents the largest unique number in the list. If no such number exists, the function returns -1.

This method efficiently identifies the targeted number by making effective use of hashing with unordered_map, ensuring good performance with respect to time complexity.

java
class Solution {

    public int findLargestUnique(int[] elements) {
        Map<Integer, Integer> countMap = new HashMap<>();

        for (int element : elements) {
            countMap.put(element, countMap.getOrDefault(element, 0) + 1);
        }

        int maxUnique = -1;

        for (int element : countMap.keySet()) {
            if (countMap.get(element) == 1 && element > maxUnique) {
                maxUnique = element;
            }
        }

        return maxUnique;
    }
}

The given Java program defines a method named findLargestUnique which is designed to find the largest unique number in an array. Here's how the method operates:

  • Initializes a HashMap to track the frequency of each number in the array.
  • Iterates over the array, updating the frequency of each element in the HashMap.
  • Sets a variable maxUnique to -1 to keep track of the maximum number encountered that appears only once in the array.
  • Iterates over the keys of the HashMap (the unique numbers). If the frequency of a number is one (indicating the number is unique) and it is greater than maxUnique, it updates maxUnique.
  • Finally, the method returns the value of maxUnique. If no unique number exists, it returns -1.

This solution effectively identifies the largest unique number by leveraging the hashing capability of HashMap to count occurrences and then filter out the largest number that only appears once.

python
class Solution:
    def findMaxUniqueNum(self, array: List[int]) -> int:
        # Creates a dictionary to count each number's occurrences
        count_dict = Counter(array)

        # Returns maximum unique number or -1 if no unique number exists
        return max(
            (number for number, occurrence in count_dict.items() if occurrence == 1),
            default=-1,
        )

The solution provided defines a method in Python to find the largest unique number from a list of integers. The method findMaxUniqueNum uses the following process:

  1. Initialize count_dict using the Counter class from the collections module to count the frequency of each number in the input list array.
  2. Utilize a generator expression to iterate over count_dict. This expression checks for numbers that appear exactly once (occurrence == 1).
  3. Apply the max function to the generator expression to get the largest number that is unique, or returns -1 if no unique numbers exist.

This method is efficient in handling lists where numbers may repeat and provides a direct approach to identifying the highest number that does not repeat in the list.

Comments

No comments yet.