Skip to main content

Biggest Single Number - Solution & Explanation

EasyDatabase14 min readAsked at: Amazon, Google, Bloomberg
Practice this problem

Problem Statement

Table: MyNumbers

+-------------+------+
| Column Name | Type |
+-------------+------+
| num         | int  |
+-------------+------+
This table may contain duplicates (In other words, there is no primary key for this table in SQL).
Each row of this table contains an integer.

 

A single number is a number that appeared only once in the MyNumbers table.

Find the largest single number. If there is no single number, report null.

The result format is in the following example.

 

Example 1:

Input: 
MyNumbers table:
+-----+
| num |
+-----+
| 8   |
| 8   |
| 3   |
| 3   |
| 1   |
| 4   |
| 5   |
| 6   |
+-----+
Output: 
+-----+
| num |
+-----+
| 6   |
+-----+
Explanation: The single numbers are 1, 4, 5, and 6.
Since 6 is the largest single number, we return it.

Example 2:

Input: 
MyNumbers table:
+-----+
| num |
+-----+
| 8   |
| 8   |
| 7   |
| 7   |
| 3   |
| 3   |
| 3   |
+-----+
Output: 
+------+
| num  |
+------+
| null |
+------+
Explanation: There are no single numbers in the input table so we return null.

Approach Overview

Problem Overview: You are given a list (or database column) of integers. The task is to return the largest number that appears exactly once. If every number appears more than once, the result should be null. The core challenge is identifying unique values while still tracking the maximum among them.

Approach 1: Using HashMap / Dictionary for Count Tracking (Time: O(n), Space: O(n))

This approach performs a frequency count of each number using a hash map. First iterate through the array (or records) and update a dictionary where key = number and value = frequency. After building the frequency map, iterate through all entries and check which numbers have a count of 1. Track the maximum among those unique numbers. Hash lookups run in constant time, so the entire counting and scanning process remains linear.

The key insight is separating the problem into two steps: frequency counting and maximum selection. This approach works for any unsorted dataset and avoids repeated scanning of the array. It is the most straightforward implementation in languages like Python, Java, and C++ using built-in map structures.

Approach 2: Using Sorting for Efficient Search (Time: O(n log n), Space: O(1) or O(n))

This method first sorts the numbers using a sorting algorithm. Once sorted, duplicate values appear next to each other. You can scan from the largest element toward the beginning and check whether the current value differs from both its neighbors. If a value appears exactly once, it must not match the previous or next element in the sorted order.

Starting the scan from the end allows you to stop immediately when the first valid unique number is found, which guarantees it is the largest. Sorting increases time complexity to O(n log n), but the logic after sorting becomes very simple and avoids additional data structures. This approach is useful when the dataset is already sorted or when memory usage must remain minimal.

Problems like this often appear in database-style questions where aggregation and uniqueness are required. Understanding counting patterns with hash maps or ordering with sorting helps solve many similar interview problems.

Recommended for interviews: The hash map counting approach is usually expected because it achieves O(n) time with straightforward logic. Mentioning the sorting alternative demonstrates awareness of tradeoffs, but interviewers typically prefer the linear-time solution since it scales better for large datasets.

Approach 1: Using HashMap/Dictionary for Count Tracking

This approach involves using a hash map or dictionary to count the number of occurrences of each integer in the array. After counting, we identify numbers that appeared only once and find the maximum among them. This way, we can efficiently determine the largest single number.

In this C code, we define a structure to store the integers and their counts. We iterate through the array, counting occurrences. After populating the list, we sort it and look for the largest number with a count of 1.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) in worst case for counting and sorting (since sorting involves iterating the elements list).
Space Complexity: O(n) for storing numbers and their counts.

Try this approach in the editor →

Approach 2: Using Sorting for Efficient Search

This approach involves sorting the numbers first. After sorting, we can iterate through the list to count numbers that appear consecutively more than once and skip them. The largest number with only one occurrence is then the answer.

In this C solution, we first sort the array in descending order. This allows us to iterate and find the largest number that is not duplicated (previous or next number does not match).

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) as we sort in place.

Try this approach in the editor →

Approach 3: Grouping and Subquery

We can first group the MyNumbers table by num and count the number of occurrences of each number. Then, we can use a subquery to find the maximum number among the numbers that appear only once.

Code

MySQL

Try this approach in the editor →

Approach 4: Grouping and `CASE` Expression

Similar to Solution 1, we can first group the MyNumbers table by num and count the number of occurrences of each number. Then, we can use a CASE expression to find the numbers that appear only once, sort them in descending order by number, and take the first one.

Code

MySQL

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using HashMap/Dictionary for Count Tracking

Time Complexity: O(n^2) in worst case for counting and sorting (since sorting involves iterating the elements list).
Space Complexity: O(n) for storing numbers and their counts.

Using Sorting for Efficient Search

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) as we sort in place.

Grouping and Subquery—
Grouping and `CASE` Expression—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
HashMap / Dictionary Frequency CountO(n)O(n)General case when input is unsorted and fast lookup is needed
Sorting + Neighbor CheckO(n log n)O(1) to O(n)When the data is already sorted or when minimizing extra data structures

Video Solution

LeetCode Interview SQL Question with Detailed Explanation | Practice SQL | LeetCode 619 • Everyday Data Science • 17,577 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Biggest Single Number easy or hard?
Biggest Single Number is classified as an Easy problem. The core idea is simple frequency counting using a hash map or identifying unique values after sorting, both of which are common beginner-level patterns in coding interviews.
Biggest Single Number Python/Java solution
In Python, use a dictionary or collections.Counter to count frequencies and then compute the maximum number with count 1. In Java, use a HashMap<Integer, Integer> to store counts and iterate through the entry set to find the largest key with value equal to one.
How to solve Biggest Single Number in O(n)?
Iterate through the numbers and store frequencies in a hash map where each number maps to its count. Then iterate through the map and track the maximum key whose value equals 1. Because both passes are linear and hash lookups are constant time, the overall complexity remains O(n).
What is the best approach for Biggest Single Number?
The most efficient approach uses a hash map (dictionary) to count the frequency of each number. After counting, iterate through the map and select the largest value with frequency equal to 1. This method runs in O(n) time with O(n) space and works for any unsorted dataset.
Is Biggest Single Number asked at Google/Amazon/Meta?
Problems involving frequency counting and identifying unique elements are common in interviews at companies like Amazon, Google, and Meta. While this exact question is from a database-focused problem set, the underlying pattern using hash maps frequently appears in coding interviews.
What data structure is used in Biggest Single Number?
A hash map (dictionary) is the primary data structure used to store the frequency of each number. It allows constant-time insertion and lookup, making it ideal for counting occurrences and quickly identifying numbers that appear exactly once.
What is the time complexity of Biggest Single Number?
The optimal solution runs in O(n) time using a hash map to count occurrences and then scanning the map for the largest unique value. A sorting-based alternative takes O(n log n) time because the array must be sorted before checking for unique elements.

Ready to solve this problem?

Practice Biggest Single Number with our built-in code editor and test cases.

Practice on FleetCode