Signup/Sign In

Java Program To Count the Number of Occurrence of an Element

In this tutorial, we will learn how to find the occurrence of an element in an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: 3 2 1 4 5 6 3 7

Output:

Element to be searched: 3

The occurrence of the element: 2

Program 1: Find the occurrence of an Element in an Array

In this method, we will see how to find the occurrence of each element in an array using loops.

Algorithm

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Enter the element whose frequency you want to know.
  7. Declare an occurrence variable and initialize it to 0.
  8. Using a for loop traverse through all the elements of the array.
  9. If the element is matched with the array element then increment the occurrence.
  10. Print the occurrence of each element.
  11. Stop

The below program demonstrates how to find the occurrence of each element in an array using loops.

/*Java Program to find the occurence of each element in an array*/
import java.util.*;  
import java.util.Arrays; 

//Driver Code
public class Main  
{  
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      
        //Enter the element whose frequency you want to know
        System.out.println("Enter the element whose frequency you want to know");
        int ele=sc.nextInt();
        int occ=0;
        
        // Traverse through map and check frequencies 
        for(int i=0;i<n;i++)
        {
            if(ele==arr[i])
            {
                occ++; //Increment the occurrence once the element is found
            }
        }
        //Print the element and its frequency
        System.out.println(ele+" occurred "+occ+" times ");
        
       
   }
}


Enter the total number of elements 10
Enter the elements of the array 2 7 6 7 7 8 9 5 7 9
Enter the element whose frequency you want to know 7
7 occurred 4 times

Program 2: Find the occurrence of an Element in an Array

In this method, we will see how to find the occurrence of each element in an array using a hashmap.

Algorithm

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Insert all array elements in the hashmap.
  7. Traverse through array elements and count frequencies using a for loop.
  8. Enter the element whose frequency you want to know.
  9. Again traverse through the map and check frequencies.
  10. Print the frequency of the element.
  11. Stop

The below program demonstrates how to find the occurrence of each element in an array using a hash map.

/*Java Program to find the occurence of each element in an array*/
import java.util.*;  
import java.util.Arrays; 

//Driver Code
public class Main  
{  
   public static void main(String args[])   
   {  
       Scanner sc=new Scanner(System.in);

      int n;    //Declare array size
      System.out.println("Enter the total number of elements ");
      n=sc.nextInt();     //Initialize array size

      int arr[]=new int[n];   //Declare array
      System.out.println("Enter the elements of the array ");
      for(int i=0; i<n ;i++)     //Initialize array
      {
          arr[i]=sc.nextInt();
      }
      
      Map<Integer, Integer> mp = new HashMap<>(); 
       // Traverse through array elements and count frequencies
       for (int i = 0; i < n; i++) 
        { 
            if (mp.containsKey(arr[i]))  
            { 
                mp.put(arr[i], mp.get(arr[i]) + 1); 
            }  
            else
            { 
                mp.put(arr[i], 1); 
            } 
        } 
        
        //Enter the element whose frequency you want to know
        System.out.println(“Enter the element whose frequency you want to know”);
        int ele=sc.nextInt();
        int occ=0;
        
        // Traverse through map and check frequencies 
        for (Map.Entry<Integer, Integer> entry : mp.entrySet()) 
        { 
            if(ele==entry.getKey())
            {
                occ=entry.getValue();
            }
        }
        //Print the element and its frequency
        System.out.println(ele+" occurred "+occ+" times ");
        
       
   }
}


Enter the total number of elements 10
Enter the elements of the array 8 7 6 59 7 9 9 5 7 9
Enter the element whose frequency you want to know 9
9 occurred 3 times



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.