Problem, as an interviewer would state it: "Count how many times each distinct element appears in an array. Discuss edge cases, runtime, and alternative approaches."
Examples:
count(["a", "b", "a", "c", "a", "b"]); // a: 3, b: 2, c: 1
Clarifying questions expected:
- Should
1and"1"be counted separately? What aboutNaN? - Are the elements guaranteed to be safe strings, or could one be the word
constructor? - Do you also want the results sorted by frequency, or just the counts?
Code / implementation expected: Yes — real, direct proof of the correct counter and of exactly how the naive object counter goes wrong.