Number Distribution (Frequency Count)
Frequency Counting / HashingSolved Oct 26, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an array of integers, count and print the frequency (number of occurrences) of each distinct element in the array.ReadHide
Problem Description
Given an array of integers, count and print the frequency (number of occurrences) of each distinct element in the array.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a hash map to count the occurrences of each number. Then, iterate through the map to find the counts of required numbers.
Approaches
1. Hashing/Frequency Map (Optimal) - O(N) ⭐
Idea: Using a hash map (dictionary in Python) allows us to count frequencies in a single O(N) pass, with O(K) space where K is the number of distinct elements (K ≤ N).
- Initialize an empty hash map.
- Iterate through the array: for each element, increment its count in the hash map.
- Iterate through the hash map (or original array again) to print the results.
Time: O(N)
Space: O(N)
Solution code
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
import java.util.Arrays;
class Solution {
// Optimized: O(N) Time, O(N) Space - Uses HashMap for frequency count
public static void solve_number_distribution_optimized(int N, Scanner scanner) {
// Using TreeMap for sorted output, but HashMap is faster if order doesn't matter (O(N) time)
Map<Integer, Integer> counts = new TreeMap<>();
int[] nums = new int[N];
for (int i = 0; i < N; i++) {
if (!scanner.hasNextInt()) return;
int num = scanner.nextInt();
nums[i] = num;
// Increment the count for the number 'num'
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
// Print the results (element: frequency)
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
// Brute Force: O(N log N) Time, O(1) or O(N) Space - Sorts the array and iterates
public static void solve_number_distribution_brute(int N, Scanner scanner) {
int[] nums = new int[N];
for (int i = 0; i < N; i++) {
if (!scanner.hasNextInt()) return;
nums[i] = scanner.nextInt();
}
// Sort the array (O(N log N))
Arrays.sort(nums);
if (N == 0) return;
int currentElement = nums[0];
int count = 0;
for (int i = 0; i < N; i++) {
if (nums[i] == currentElement) {
count++;
} else {
System.out.println(currentElement + ": " + count);
currentElement = nums[i];
count = 1;
}
}
// Print the last element's count
System.out.println(currentElement + ": " + count);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (!scanner.hasNextInt()) return;
int N = scanner.nextInt();
// Since input can only be read once, you'd typically choose one solver.
// We'll use the optimized version and assume the remaining input stream follows N.
solve_number_distribution_optimized(N, scanner);
scanner.close();
}
}Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language