Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Introduction to Counting Pattern

You are given an array and asked how many elements appear as often as the most frequent value.

[1, 2, 2, 3, 1, 4]
1 appears twice, 2 appears twice, 3 once, 4 once
the highest count is 2, and two values reach it

The direct approach counts each value by scanning the whole array for it. That is O(N²), and it counts the same value again every time it appears.

Every one of those scans asks the same question. Answer it once instead. Walk the array a single time and keep a tally, adding one to the entry for each value you pass.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content