Frequency Counting Pattern Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you recognize the pattern and its O(n) time / O(k) space tradeoff over brute force
Using nested loops to count (O(n^2)) when one map pass works, or a HashMap where a 26/256 int array is faster
Mention using a fixed int[26] or int[256] for known character sets instead of a HashMap
Interview-ready guide
Interview answer
The frequency counting pattern is one of the most reusable tools in interviews.
The idea is simple: I make a single pass over the input and tally how many times each element appears, usually in a hash map keyed by the element with the count as the value.
Once I have those counts, a whole family of questions becomes easy: detecting duplicates, checking if two strings are anagrams, finding the first non-repeating character, finding the top K most frequent elements, the majority element, or whether a string can be rearranged into a palindrome. The reason this pattern matters is complexity.
A brute force approach to many of these problems compares every pair of elements, which is O of n squared. Counting frequencies first is O of n time and O of k space, where k is the number of distinct elements. So I trade a little memory for a big speedup.
One refinement I'd mention: when the set of possible elements is small and known, like lowercase English letters or ASCII characters, I don't even need a hash map. I can use a fixed-size integer array, int of twenty-six for lowercase letters, and index it with the character minus the letter a. That's faster and lighter than a hash map because it avoids hashing and boxing.
In Java, a clean way to increment counts in a map is the merge method, calling merge with the key, one, and Integer sum, which either inserts one or adds one to the existing count. So whenever a problem mentions counts, duplicates, most common, or anagrams, frequency counting is the first thing I reach for.
Frequency Counting Pattern Java | Fresher Interview
Direct Answer
The frequency counting pattern uses a hash map (or a fixed-size array for known alphabets) to tally how many times each element appears in a single pass. It turns many 'count / compare / find duplicate / most common' problems from O(n^2) nested scanning into O(n) time with O(k) space, where k is the number of distinct elements. Reach for it whenever you need counts, duplicates, anagrams, or 'majority/top' answers.
Deep Dive & Architecture
Frequency counting separates a problem into two passes: first build reliable counts, then use those counts to answer the actual question. This avoids searching the whole input again for every value.
Example: banana
Start with an empty map. Reading the characters in order produces b=1, a=1, n=1, then a=2, n=2, and finally a=3. The completed map is {b=1, a=3, n=2}. From the same data, code can find repeated characters, the most frequent character, or compare it with another word's counts.
Choose the storage from the key range. A HashMap<Character, Integer> works for an open or unknown set of characters. For guaranteed lowercase English letters, int[26] is simpler and avoids hashing; index c - 'a' stores the count. The array is not appropriate when uppercase letters, arbitrary Unicode, or general objects are allowed.
Building counts is O(n). Reading or comparing the count structure usually costs O(k), where k is the number of distinct keys or the fixed alphabet size. Memory is O(k). The method is useful for anagrams, duplicate detection, first unique values, majority checks, and top-frequency problems.
Code Example
// HashMap version - works for any element type
Map<Character, Integer> freq = new HashMap<>();
for (char c : "banana".toCharArray())
freq.merge(c, 1, Integer::sum);
// freq = {b=1, a=3, n=2}
// Fixed-array version - faster for lowercase letters only
int[] count = new int[26];
for (char c : "banana".toCharArray()) count[c - 'a']++;
// count['a'-'a']=3, count['b'-'a']=1, count['n'-'a']=2
merge(c, 1, Integer::sum) increments or initializes to 1 in one call.
Common Mistakes & Pitfalls
- Nested loops to count: O(n^2) when a single map pass is O(n).
- HashMap for fixed alphabets: an
int[26]/int[256]is faster and lighter. - Forgetting to handle absent keys: use
getOrDefaultormerge, notgetthen null-check. - Off-by-one with
c - 'a': only valid for lowercase a-z input.
Check your understanding
Answer these without looking back at the article.
- How do you find the top K most frequent elements in an array?
- How do you group strings that are anagrams together?
- How do you find the first non-repeating character in a string?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.