C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to find the frequency of each element of an array.
ExplanationIn this program, we need to count the occurrence of each unique element present in the array. One of the approach to resolve this problem is to maintain one array to store the counts of each element of the array. Loop through the array and count the occurrence of each element and store it in another array fr.
In the above array, 1 has appeared 1 time, so, the frequency of 1 is 1. Similarly, 2 has appeared 4 times. The frequency of 2 is 4 and so on. Algorithm
SolutionPython
#Initialize array
arr = [1, 2, 8, 3, 2, 2, 2, 5, 1];
#Array fr will store frequencies of element
fr = [None] * len(arr);
visited = -1;
for i in range(0, len(arr)):
count = 1;
for j in range(i+1, len(arr)):
if(arr[i] == arr[j]):
count = count + 1;
#To avoid counting same element again
fr[j] = visited;
if(fr[i] != visited):
fr[i] = count;
#Displays the frequency of each element present in array
print("---------------------");
print(" Element | Frequency");
print("---------------------");
for i in range(0, len(fr)):
if(fr[i] != visited):
print(" " + str(arr[i]) + " | " + str(fr[i]));
print("---------------------");
Output: ----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------
C
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {1, 2, 8, 3, 2, 2, 2, 5, 1};
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Array fr will store frequencies of element
int fr[length];
int visited = -1;
for(int i = 0; i < length; i++){
int count = 1;
for(int j = i+1; j < length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
//Displays the frequency of each element present in array
printf("---------------------\n");
printf(" Element | Frequency\n");
printf("---------------------\n");
for(int i = 0; i < length; i++){
if(fr[i] != visited){
printf(" %d", arr[i]);
printf(" | ");
printf(" %d\n", fr[i]);
}
}
printf("---------------------\n");
return 0;
}
Output: ----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------
JAVA
public class Frequency {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};
//Array fr will store frequencies of element
int [] fr = new int [arr.length];
int visited = -1;
for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
//Displays the frequency of each element present in array
System.out.println("---------------------");
System.out.println(" Element | Frequency");
System.out.println("---------------------");
for(int i = 0; i < fr.length; i++){
if(fr[i] != visited)
System.out.println(" " + arr[i] + " | " + fr[i]);
}
System.out.println("---------------------");
}
}
Output: ----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------
C#
using System;
public class Frequency
{
public static void Main()
{
//Initialize array
int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1};
//Array fr will store frequencies of element
int [] fr = new int [arr.Length];
int visited = -1;
for(int i = 0; i < arr.Length; i++){
int count = 1;
for(int j = i+1; j < arr.Length; j++){
if(arr[i] == arr[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
//Displays the frequency of each element present in array
Console.WriteLine("---------------------");
Console.WriteLine(" Element | Frequency");
Console.WriteLine("---------------------");
for(int i = 0; i < fr.Length; i++){
if(fr[i] != visited)
Console.WriteLine(" " + arr[i] + " | " + fr[i]);
}
Console.WriteLine("---------------------");
}
}
Output: ----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------
PHP
<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array
$arr = array(1, 2, 8, 3, 2, 2, 2, 5, 1);
//Array fr will store frequencies of element
$fr = array_fill(0, count($arr), 0);
$visited = -1;
for($i = 0; $i < count($arr); $i++){
$count = 1;
for($j = $i+1; $j < count($arr); $j++){
if($arr[$i] == $arr[$j]){
$count++;
//To avoid counting same element again
$fr[$j] = $visited;
}
}
if($fr[$i] != $visited)
$fr[$i] = $count;
}
//Displays the frequency of each element present in array
print("-------------------------<br>");
print("�Element | Frequency<br>");
print("-------------------------<br>");
for($i = 0; $i < count($fr); $i++){
if($fr[$i] != $visited){
//str_repeat('�', 6) is used to add extra whitespace in output
print(str_repeat('�', 6) . $arr[$i] );
print(str_repeat('�', 7) . "|" . str_repeat('�', 7) . $fr[$i]);
print("<br>");
}
}
print("-------------------------");
?>
</body>
</html>
Output: ----------------------------
Element | Frequency
----------------------------
1 | 2
2 | 4
8 | 1
3 | 1
5 | 1
----------------------------
Next Topic#
|