Watch 10 video solutions for Biggest Single Number, a easy level problem involving Database. This walkthrough by Everyday Data Science has 16,612 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |