Biggest Single Number - Problem

You are given a table MyNumbers that contains integers. Some numbers may appear multiple times while others appear only once.

Goal: Find the largest number that appears exactly once in the table. If no such number exists, return null.

Table Structure:

  • num (int): An integer value

Note: The table has no primary key, so duplicate values are allowed.

Table Schema

MyNumbers
Column Name Type Description
num int Integer value that may appear multiple times
Primary Key: none
Note: Table allows duplicates, no primary key defined

Input & Output

Example 1 — Multiple Singles Exist
Input Table:
num
8
3
5
4
3
6
Output:
num
8
💡 Note:

Numbers 8, 5, 4, and 6 appear exactly once, while 3 appears twice. The largest single number is 8.

Example 2 — All Numbers Repeat
Input Table:
num
1
1
2
2
Output:
num
💡 Note:

Both numbers 1 and 2 appear twice. Since no number appears exactly once, the result is null.

Example 3 — Single Number Only
Input Table:
num
10
Output:
num
10
💡 Note:

Only one number exists and it appears once, so 10 is the biggest single number.

Constraints

  • 1 ≤ MyNumbers.length ≤ 1000
  • -1000 ≤ num ≤ 1000

Visualization

Tap to expand
Biggest Single Number INPUT MyNumbers Table num 8 8 3 3 1 4 5 6 Numbers: 8,8,3,3,1,4,5,6 Find largest appearing once (Return null if none exists) ALGORITHM STEPS 1 GROUP BY num Count occurrences of each 2 HAVING COUNT = 1 Filter single numbers only Count Results: 8: 2 (X) 3: 2 (X) 1: 1 (OK) 4: 1 (OK) 5: 1 (OK) 6: 1 (OK) Singles: 1, 4, 5, 6 3 ORDER BY num DESC Sort descending 4 LIMIT 1 Take first (largest) FINAL RESULT -- SQL Query SELECT MAX(num) AS num FROM MyNumbers GROUP BY num HAVING COUNT(num) = 1 ORDER BY 1 DESC LIMIT 1; Processing singles: 1, 4, 5, 6 6 5 4 1 (sorted) OUTPUT 6 Largest single number: 6 (appears exactly once) Key Insight: Use GROUP BY with HAVING COUNT(num) = 1 to filter numbers appearing exactly once. Then apply MAX() or ORDER BY DESC with LIMIT 1 to get the largest single number. If no single number exists, the query naturally returns NULL (empty result set). TutorialsPoint - Biggest Single Number | Optimal Solution
Asked in
Amazon 15 Microsoft 12
28.0K Views
Medium Frequency
~8 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen