DATA STRUCTURES PROGRAM • HEAPS, HASHING & DISJOINT SETS
Count Frequencies with a Hash-Style Table
Learn how to count frequencies with a hash-style table using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
2 occurs 2 time(s) 3 occurs 1 time(s) 4 occurs 3 time(s)
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int values[] = {4, 2, 4, 3, 2, 4};
int frequency[11] = {0};
for (int index = 0; index < 6; index++) frequency[values[index]]++;
for (int value = 0; value <= 10; value++)
if (frequency[value] > 0) printf("%d occurs %d time(s)\n", value, frequency[value]);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
2 occurs 2 time(s) 3 occurs 1 time(s) 4 occurs 3 time(s)
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use each bounded integer as an array index and increment its counter.
- Display the computed result.
Use each bounded integer as an array index and increment its counter.
EFFICIENCY
Time and space complexity
Time complexity
O(n + range)
Auxiliary space
O(n)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
