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.
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.
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.
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.
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).
Time Complexity: O(n log n) due to sorting.
Space Complexity: O(1) as we sort in place.
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.
MySQL
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.
MySQL
| Approach | Complexity |
|---|---|
| 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). |
| Using Sorting for Efficient Search | Time Complexity: O(n log n) due to sorting. |
| Grouping and Subquery | — |
| Grouping and `CASE` Expression | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| HashMap / Dictionary Frequency Count | O(n) | O(n) | General case when input is unsorted and fast lookup is needed |
| Sorting + Neighbor Check | O(n log n) | O(1) to O(n) | When the data is already sorted or when minimizing extra data structures |
LeetCode Interview SQL Question with Detailed Explanation | Practice SQL | LeetCode 619 • Everyday Data Science • 16,612 views views
Watch 9 more video solutions →Practice Biggest Single Number with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor