Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Top K Frequent Words
Bookmark
Sorting
Min-Heap
Bucket Sort
Input
Standard
Larger
Custom
words
=
[i, love, leetcode, i, love, coding]
,
k
=
2
INPUT WORDS
i
love
leetcode
i
love
coding
find the k most frequent words
INPUT WORDS
i
love
leetcode
i
love
coding
find the k most frequent words
INPUT WORDS
i
love
leetcode
i
love
coding
count how often each word appears
INPUT WORDS
i
love
leetcode
i
love
coding
i
FREQUENCY MAP
i
:
1
count '
i
' →
1
INPUT WORDS
i
love
leetcode
i
love
coding
i
FREQUENCY MAP
i
:
1
love
:
1
leetcode
:
1
count '
leetcode
' →
1
INPUT WORDS
i
love
leetcode
i
love
coding
i
FREQUENCY MAP
i
:
2
love
:
1
leetcode
:
1
count '
i
' →
2
INPUT WORDS
i
love
leetcode
i
love
coding
i
FREQUENCY MAP
i
:
2
love
:
2
leetcode
:
1
count '
love
' →
2
INPUT WORDS
i
love
leetcode
i
love
coding
i
FREQUENCY MAP
i
:
2
love
:
2
leetcode
:
1
coding
:
1
count '
coding
' →
1
INPUT WORDS
i
love
leetcode
i
love
coding
FREQUENCY MAP
i
:
2
love
:
2
leetcode
:
1
coding
:
1
SORTED CANDIDATES (count desc, then a → z)
i
:
2
love
:
2
coding
:
1
leetcode
:
1
RESULT (top 2)
i
take '
i
' (
1
/ 2)
INPUT WORDS
i
love
leetcode
i
love
coding
FREQUENCY MAP
i
:
2
love
:
2
leetcode
:
1
coding
:
1
SORTED CANDIDATES (count desc, then a → z)
i
:
2
love
:
2
coding
:
1
leetcode
:
1
RESULT (top 2)
i
love
take '
love
' (
2
/ 2)
INPUT WORDS
i
love
leetcode
i
love
coding
FREQUENCY MAP
i
:
2
love
:
2
leetcode
:
1
coding
:
1
SORTED CANDIDATES (count desc, then a → z)
i
:
2
love
:
2
coding
:
1
leetcode
:
1
RESULT (top 2)
i
love
top 2 = [i, love]
algo
master
.
io
Step:
Find the k most frequent words by sorting every candidate
0 / 11
Input
Standard
Larger
Custom
words
=
[i, love, leetcode, i, love, coding]
,
k
=
2
0 / 11
algo
master
.
io
Step:
Find the k most frequent words by sorting every candidate