Algorithms and Data Structures · Junior · Question 61 of 75

How do you count how many times each word appears in a list in O(n)?

Explanation

One pass with counts.set(w, (counts.get(w) ?? 0) + 1) is O(n) on average. Frequency counting with a hash map is one of the most common building blocks in interview problems.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 61

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz