3.48 Maximum Number of Repetitions
3.48.1 Problem Metadata
- Platform: Firecode
- Difficulty: Medium
- URL: N/A
- Tags:
- Techniques: Counting, Array
3.48.2 Description
Given an array of integers where each value lies in [0, n-1], return the integer that appears most frequently. The algorithm must run in O(n) time and O(1) extra space.
3.48.5 Solution - In-place Counting
3.48.5.1 Walkthrough
Exploit the bounded value range by using the input array itself as a hash table. For each value a[i], increment a[a[i] % n] by n. After one pass, the count of value v equals a[v] / n. Track the index with the maximum total.