Q. Write a Program to find the frequency of each element in an array.
The program will find the frequency of each element in the given array. It will take array of integer as input and it will print frequency of each element in the array.
For Example :-
Input : arr[] = {4, 2, 2, 8, 3, 3, 1}
Output =
Frequency of 1 is 1
Frequency of 2 is 2
Frequency of 3 is 2
Frequency of 4 is 1
Frequency of 8 is 1
Algorithm
To solve this problem, we can use the following algorithm: START Step 1 : Take an integer array as input of size n. Step 2 : Initialize a frequency array of size n with all elements as 0. Step 3 : Traverse the input array and for each element a[i]: a. Check if element already visited or not. If element already visited, then move to the next element. b. If element not visited, then mark the element visited by setting the visited flag as 1. c. Traverse the input array again and for each element a[j]: i. If the element a[i] is equal to the element a[j], then increment the frequency array at index i. Step 4 : Print the frequency of each element in the frequency array. STOP
Program to find the frequency of each element in an array
#include<stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE], freq[MAX_SIZE]; int n, i, j, count; printf("Enter size of array: "); scanf("%d", &n); // Input elements in array printf("Enter elements in array:\n"); for(i=0; i<n; i++) { scanf("%d", &arr[i]); freq[i] = -1; // Initialize frequency array as -1 } // Logic to find frequency of each element for(i=0; i<n; i++) { count = 1; for(j=i+1; j<n; j++) { if(arr[i] == arr[j]) { count++; freq[j] = 0; // Mark element as visited } } if(freq[i] != 0) { freq[i] = count; } } // Print frequency of each element printf("\nFrequency of all elements of array : \n"); for(i=0; i<n; i++) { if(freq[i] != 0) { printf("%d occurs %d times\n", arr[i], freq[i]); } } return 0; }
#include <iostream> #include <unordered_map> using namespace std; int main() { int n; cout << "Enter the size of array: "; cin >> n; int arr[n]; cout << "Enter the elements of the array: "; for (int i = 0; i < n; i++) { cin >> arr[i]; } // using unordered_map to store the frequency of each element unordered_map<int, int> freqMap; for (int i = 0; i < n; i++) { freqMap[arr[i]]++; } // printing the frequency of each element cout << "Frequency of each element in the array: " << endl; for (auto it = freqMap.begin(); it != freqMap.end(); it++) { cout << it->first << " : " << it->second << endl; } return 0; }
import java.util.HashMap; public class FrequencyOfElements { public static void main(String[] args) { int[] arr = {10, 20, 20, 10, 10, 30, 50, 10, 20}; // Create a HashMap to store the frequency of each element HashMap<Integer, Integer> freqMap = new HashMap<>(); // Iterate over the array and update the frequency of each element in the HashMap for(int i=0; i<arr.length; i++) { if(freqMap.containsKey(arr[i])) { freqMap.put(arr[i], freqMap.get(arr[i])+1); } else { freqMap.put(arr[i], 1); } } // Print the frequency of each element for(int key : freqMap.keySet()) { System.out.println(key + " occurs " + freqMap.get(key) + " times"); } } }
arr = [1, 2, 3, 2, 1, 2, 4, 5, 4, 1] freq = {} for i in arr: if i in freq: freq[i] += 1 else: freq[i] = 1 for key, value in freq.items(): print(key, ":", value)
using System; class Program { static void Main(string[] args) { int[] arr = {1, 2, 3, 1, 2, 3, 4, 5, 6, 5}; int n = arr.Length; int[] freq = new int[n]; int visited = -1; for(int i = 0; i < n; i++) { int count = 1; for(int j = i + 1; j < n; j++) { if(arr[i] == arr[j]) { count++; freq[j] = visited; } } if(freq[i] != visited) { freq[i] = count; } } Console.WriteLine("Element\tFrequency"); for(int i = 0; i < n; i++) { if(freq[i] != visited) { Console.WriteLine(arr[i] + "\t" + freq[i]); } } } }
#include<stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE], freq[MAX_SIZE]; int n, i, j, count; printf("Enter size of array: "); scanf("%d", &n); // Input elements in array printf("Enter elements in array:\n"); for(i=0; i<n; i++) { scanf("%d", &arr[i]); freq[i] = -1; // Initialize frequency array as -1 } // Logic to find frequency of each element for(i=0; i<n; i++) { count = 1; for(j=i+1; j<n; j++) { if(arr[i] == arr[j]) { count++; freq[j] = 0; // Mark element as visited } } if(freq[i] != 0) { freq[i] = count; } } // Print frequency of each element printf("\nFrequency of all elements of array : \n"); for(i=0; i<n; i++) { if(freq[i] != 0) { printf("%d occurs %d times\n", arr[i], freq[i]); } } return 0; }
Output
arr = [1, 2, 3, 2, 4, 3, 1, 5, 1, 4, 1, 3] Element frequency: 1 : 4 2 : 2 3 : 3 4 : 2 5 : 1
Recommended Programs
Check if a number is a power of three.Program to remove all occurrences of a character in a string.