Frequency of Elements in Array in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include<stdio.h>

int main() {
    int a[100], f[100]={0}, n;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%d", &a[i]), f[a[i]]++;
    for(int i=0; i<100; i++) if(f[i]) printf("%d:%d\n", i, f[i]);
}

C Output

Input:  
6  
1 2 2 3 3 3  

Output:  
1:1  
2:2  
3:3


C++ Program

#include<iostream>
using namespace std;

int main() {
    int a[100], f[101]={0}, n;
    cin >> n;
    for(int i=0; i<n; i++) cin >> a[i], f[a[i]]++;
    for(int i=0; i<101; i++) if(f[i]) cout << i << ":" << f[i] << "\n";
}

C++ Output

Input:  
5  
4 4 5 4 5  

Output:  
4:3  
5:2


JAVA Program

import java.util.*;

class F {
    public static void main(String[] a) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), arr[] = new int[n];
        Map<Integer,Integer> f = new HashMap<>();
        for(int i=0; i<n; i++) {
            int x = sc.nextInt();
            f.put(x, f.getOrDefault(x, 0) + 1);
        }
        for(int k : f.keySet())
            System.out.println(k + ":" + f.get(k));
    }
}

JAVA Output

Input:  
4  
2 2 1 2  

Output:  
1:1  
2:3


Python Program

a = list(map(int, input().split()))
for i in set(a): print(i, ":", a.count(i))

Python Output

Input:  
1 2 1 2 2  

Output:  
1 : 2  
2 : 3


In-Depth Learning – Complete Concept in Paragraphs

What Does "Frequency of Elements" Mean?

In programming, the frequency of an element refers to how many times it appears in a data set, such as an array or list. If an element appears more than once, its frequency will be greater than 1. This is a fundamental operation in data analysis, used in areas like counting votes, log analysis, surveys, word counting, and data compression.


How the Code Works

The logic across all languages follows a similar approach:

  • Read the array.

  • Use a frequency counter (array or map/dictionary) to track how often each number appears.

  • Loop through this counter and print the element and its frequency.

In C/C++, we assume input numbers are small and use a fixed-size array (e.g., f[100]) where the index represents the element and the value at that index represents its frequency.

In Java, we use a HashMap<Integer, Integer> to store element-frequency pairs dynamically.

In Python, set(a) gets all unique elements, and a.count(i) counts their occurrences.


Example

Input:

csharp

[5, 1, 5, 3, 1, 5]

Processing:

  • 5 → appears 3 times

  • 1 → appears 2 times

  • 3 → appears 1 time

Output:

makefile

1:2 3:1 5:3

The order may vary based on the data structure (e.g., Python sets are unordered, HashMaps are not ordered by default).


Real-Life Analogy

Think of a tally counter used during voting or a survey:

  • Each time someone votes for a choice, you increment the tally for that option.

  • At the end, you want to see how many votes each option received.

That’s exactly what this program is doing — it’s tallying numbers, just like counting how many people picked "Option A", "Option B", etc.


Where and When Is It Used?

Counting frequency of elements is extremely useful in:

  • Competitive programming

  • Word or character frequency analysis

  • Survey analysis

  • Spam detection

  • Inventory systems

  • Hash-based algorithms and data mining

This is often the first step in solving bigger problems like finding duplicates, modes, majority elements, etc.


Time and Space Complexity

OperationComplexity
TimeO(n × k) where k = lookup cost (1 for array, up to n for .count() in Python)
SpaceO(n) (for map or frequency array)

Python’s .count() inside a loop is not optimal for very large data — for those cases, use collections.Counter.

Python-Specific Tip

You can solve this more efficiently using collections.Counter:

python

from collections import Counter a = list(map(int, input().split())) c = Counter(a) for k in c: print(k, ":", c[k])

This version is faster and more efficient for large datasets.


SEO-Optimized Natural Paragraph for Ranking

Looking to count the frequency of elements in an array using C, C++, Java, or Python? This tutorial offers the shortest and simplest way to write code that counts how many times each element appears in a list or array. Frequency counting is one of the most fundamental programming skills, often used in analytics, data science, and interviews. Whether you’re analyzing input data, building search engines, or just learning loops and maps, understanding how to calculate frequencies will help you solve a wide range of real-world problems. This guide uses array-based and hash-based methods with clear examples and outputs.