Biggest Single Number — LeetCode 619 Python Solution

EasyDatabase
Problem
#619
Reading time
2 min

The problem

A single number is a value that appears exactly once in the table. Report the largest single number, or null when every value appears more than once. The MyNumbers table has one row per value and may contain duplicates: num (int).

Example

MyNumbers table:

| num |
| --- |
| 12  |
| 7   |
| 12  |
| 30  |
| 4   |

Result:

| num |
| --- |
| 30  |

The values 7, 30 and 4 each appear once while 12 appears twice, so the largest single number is 30; had every value been repeated the answer would be null.

Python solution

Python
import pandas as pd

def biggest_single_number(my_numbers: pd.DataFrame) -> pd.DataFrame:
    counts = my_numbers['num'].value_counts()
    singles = counts[counts == 1]
    if singles.empty:
        return pd.DataFrame({'num': [None]})
    return pd.DataFrame({'num': [int(singles.index.max())]})

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 619. Biggest Single Number?
LeetCode 619. Biggest Single Number is rated Easy on LeetCode.
What topics does LeetCode 619. Biggest Single Number cover?
LeetCode 619. Biggest Single Number is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview