Question Bank
/

JavaScript Character Occurrence Count: Three Approaches Quiz

JavaScript Character Occurrence Count: Three Approaches Quiz

Count how often each unique character from a needle string appears in a haystack, three ways (Set + per-char count, dict tally, regex match per char), plus two companions on case folding and a single-pass tally.

Question Bank
Hard
JavaScript
quiz
strings
hash-map
interview-prep

690 views

2

Implement getCount(chars, text) using a Set to dedupe the needle, count each unique char with text.split(c).length - 1, and return an object that includes a totalCount key.

Examples

Example 1:

Input: chars = "abe", text = "hey my name is joe"
Output: { a: 1, b: 0, e: 3, totalCount: 4 }
Explanation: "a" appears once, "b" zero times, "e" three times; totalCount sums the per-key tallies.

4 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium