Magicsheet logo

Largest Unique Number

Easy
25%
Updated 8/1/2025

Asked by 1 Company

Largest Unique Number

1. What is this problem about?

The Largest Unique Number coding problem gives you an array of integers and asks you to find the largest integer that appears exactly once in the array. If no such number exists (i.e., every number is repeated at least once), you should return -1.

2. Why is this asked in interviews?

This "Easy" level question is frequently asked by Amazon as a warm-up or screening question. It tests a candidate's ability to efficiently count frequencies and filter data. It evaluates whether you can choose the right data structure (like a Hash Table) to solve the problem in linear time, which is a fundamental requirement for most large-scale data processing tasks.

3. Algorithmic pattern used

This follows the Hash Table and Sorting interview pattern. The most efficient approach uses a Hash Table (or a frequency array if the range of numbers is small). First, iterate through the array to store the count of each number in a dictionary. Then, iterate through the dictionary to find all numbers with a count of 1 and pick the maximum among them.

4. Example explanation

Array: [5, 7, 3, 9, 5, 8, 8].

  1. Count frequencies:
    • 5 appears 2 times
    • 7 appears 1 time
    • 3 appears 1 time
    • 9 appears 1 time
    • 8 appears 2 times
  2. Numbers with frequency 1: [7, 3, 9].
  3. The largest of these is 9. Result: 9.

5. Common mistakes candidates make

A common mistake is sorting the entire array first, which takes O(N log N) time. While this works, a Hash Table approach is faster at O(N). Another error is returning the "largest number" without checking if it's unique (e.g., returning 8 in the example above). Some candidates also forget to handle the empty result case correctly by returning -1.

6. Interview preparation tip

For "Array, Hash Table interview pattern" questions, always start by considering the frequency of elements. Frequency counting is a universal tool that simplifies many array-based challenges. Practice using Python's collections.Counter or similar built-in utilities in other languages to write this code quickly and cleanly.

Similar Questions