{x}
blog image

Biggest Single Number

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.

Solution Explanation for LeetCode Problem 619: Biggest Single Number

This problem requires finding the largest number that appears only once in the MyNumbers table. Both solutions presented leverage SQL's GROUP BY and COUNT functionalities to identify numbers appearing exactly once. They differ slightly in how they extract the largest single number.

Solution 1: Grouping and Subquery

This approach uses a subquery to filter for single numbers and then finds the maximum among them.

Steps:

  1. Inner Query: SELECT num FROM MyNumbers GROUP BY 1 HAVING COUNT(1) = 1. This part groups the table by the num column and filters out any groups with more than one occurrence (HAVING COUNT(1) = 1). It effectively creates a temporary table containing only the single numbers. GROUP BY 1 is a shorthand for GROUP BY num, often used for conciseness.

  2. Outer Query: SELECT MAX(num) AS num FROM (...) AS t;. This query selects the maximum value (MAX(num)) from the results of the inner query and aliases it as num. The inner query's result is aliased as t for convenient reference.

Time Complexity: The time complexity is dominated by the GROUP BY operation, which generally has a time complexity of O(N log N) or O(N) depending on the database engine's implementation (where N is the number of rows in MyNumbers). The MAX operation is then performed on a potentially smaller dataset, making its contribution negligible compared to the grouping. Therefore, the overall time complexity is O(N log N) or O(N).

Space Complexity: The space complexity depends on the size of the temporary table created during the GROUP BY operation. In the worst case (all numbers are unique), this table will be as large as the input table, leading to a space complexity of O(N).

Solution 2: Grouping and CASE Expression

This approach utilizes a CASE expression within the main query to conditionally select the number based on its count.

Steps:

  1. Grouping and Counting: FROM MyNumbers GROUP BY num. The table is grouped by num, allowing for counting occurrences of each number.

  2. Conditional Selection: SELECT CASE WHEN COUNT(1) = 1 THEN num ELSE NULL END AS num. A CASE statement checks if the count (COUNT(1)) for each group is equal to 1. If true, the number (num) is selected; otherwise, NULL is selected.

  3. Ordering and Limiting: ORDER BY 1 DESC LIMIT 1. The results are ordered in descending order by the num column (ORDER BY 1 is a shorthand for ORDER BY num), and only the top row (the largest) is returned using LIMIT 1.

Time Complexity: Similar to Solution 1, the dominant factor is the GROUP BY operation, resulting in a time complexity of O(N log N) or O(N). The CASE expression, ordering, and limiting operations have relatively smaller contributions.

Space Complexity: Similar to Solution 1, the space complexity is O(N) in the worst case, due to the need for a temporary table to store the intermediate group counts.

Code in Different Languages (MySQL)

Both solutions are provided in MySQL, as that's the language specified in the problem description. Other SQL dialects (PostgreSQL, SQL Server, etc.) would have very similar implementations.

Solution 1 (MySQL):

SELECT MAX(num) AS num
FROM
    (
        SELECT num
        FROM MyNumbers
        GROUP BY num
        HAVING COUNT(*) = 1
    ) AS t;

Solution 2 (MySQL):

SELECT
    CASE
        WHEN COUNT(*) = 1 THEN num
        ELSE NULL
    END AS num
FROM MyNumbers
GROUP BY num
ORDER BY num DESC
LIMIT 1;

Both solutions effectively solve the problem. Solution 2 might be slightly more concise in some SQL dialects, but the performance difference is usually negligible. The choice often comes down to personal preference and coding style.