Q:

Program to find the frequency of each element of an array

belongs to collection: Array Programs

0

Explanation

In 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

  1. Declare and initialize an array arr.
  2. Declare another array fr with the same size of array arr. It is used to store the frequencies of elements present in the array.
  3. Variable visited will be initialized with the value -1. It is required to mark an element visited that is, it helps us to avoid counting the same element again.
  4. The frequency of an element can be counted using two loops. One loop will be used to select an element from an array, and another loop will be used to compare the selected element with the rest of the array.
  5. Initialize count to 1 in the first loop to maintain a count of each element. Increment its value by 1 if a duplicate element is found in the second loop. Since we have counted this element and didn't want to count it again. Mark this element as visited by setting fr[j] = visited. Store count of each element to fr.
  6. Finally, print out the element along with its frequency.

Input:

arr = [1, 2, 8, 3, 2, 2, 2, 5, 1]  

Output:

Element | Frequency
       1|2
       2|4
       8|1
       3|1
       5|1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#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
----------------------------

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to left rotate the elements of an array... >>
<< Program to copy all the elements of one array into...