Domain 1EasyCORE

Largest Unique Number

The problem requires finding the largest integer in an array that appears exactly once. The input is an array of integers `A`, and the output should be the largest unique integer or -1 if no such integer exists. Key constraints include: - The length of the array is between 1 and 2000. - Each element in the array is between 0 and 1000.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array#Hash Table

1133. Largest Unique Number

Model: qwen2.5-coder:14b
Topics: Array,Hash Table
FAANG asked: 1
Acceptance rate: 67.9%


Problem Breakdown

The problem requires finding the largest integer in an array that appears exactly once. The input is an array of integers A, and the output should be the largest unique integer or -1 if no such integer exists. Key constraints include:

  • The length of the array is between 1 and 2000.
  • Each element in the array is between 0 and 1000.

Edge cases to consider:

  • An empty array (though not explicitly mentioned, it’s a good edge case to think about).
  • All elements are unique.
  • All elements are duplicates.
  • The largest number is also the most frequent.

Intuition

A brute-force approach would involve checking each element in the array to see if it appears exactly once. This could be done by counting occurrences of each element, which would take O(n^2) time complexity due to nested loops (one for counting and one for comparison).

The key insight is that we can use a hash table (dictionary in Python) to count occurrences of each element in a single pass, reducing the time complexity to O(n). After populating the hash table, we iterate through it to find the largest number with a count of 1.

Approach

  1. Initialize an empty dictionary count_map to store the frequency of each integer.
  2. Iterate through the array A and populate count_map where keys are integers from the array and values are their respective counts.
  3. Initialize a variable largest_unique to -1, which will store the largest unique number found.
  4. Iterate through the items in count_map. For each item, if the count is 1 (i.e., the integer appears exactly once), update largest_unique if this integer is greater than the current value of largest_unique.
  5. Return largest_unique.

Complexity Analysis

  • Time Complexity: O(n) where n is the length of the array A. This is because we make two passes over the array: one to populate the hash table and another to find the largest unique number.
  • Space Complexity: O(n) in the worst case, where all elements are unique and stored in the hash table.

Solution

def largestUniqueNumber(A):
    count_map = {}
    
    # Count occurrences of each number
    for num in A:
        if num in count_map:
            count_map[num] += 1
        else:
            count_map[num] = 1
    
    largest_unique = -1
    
    # Find the largest unique number
    for num, count in count_map.items():
        if count == 1 and num > largest_unique:
            largest_unique = num
    
    return largest_unique

Edge Cases Handled

  • Empty Array: The function will correctly return -1 as there are no elements to check.
  • All Unique Elements: The function will return the maximum element in the array.
  • All Duplicates: The function will return -1 as no unique elements exist.
  • Largest Number is Most Frequent: The function will correctly identify and return the largest number that appears exactly once.