Java Program to Count Occurrence of an Element in an Array

Write a Java program to count occurrence of an element in an array using for loop. This Java program accepts the size, array of elements, and the item to search for.

The for loop iterate each item in an array, and the if statement compares that item with the number. if they both match, the occurrence variable increment by one. Finally, we are printing the occurrence variable as an output.

package NumPrograms;

import java.util.Scanner;

public class ArrayCountOccrence1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, num, occr = 0;
		
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the Array size = ");
		Size = sc.nextInt();
		
		int[] arr = new int[Size];
		
		System.out.format("Enter the Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			arr[i] = sc.nextInt();
		}
		
		System.out.print("Please Enter the Array Item to Know = ");
		num = sc.nextInt();
		
		for(i = 0; i < arr.length; i++) 
		{
			if(arr[i] == num)
			{
				occr++;
			}
		}
		
		System.out.println(num + " Occurred " + occr + " Times.");
	}
}
Java Program to Count Occurrence of an Element in an Array

This Java program counts the occurrence of an element in an array using a while loop.

package NumPrograms;

import java.util.Scanner;

public class ArrayCountOccrence2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, num, occr = 0;
		
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the size = ");
		Size = sc.nextInt();
		
		int[] arr = new int[Size];
		
		System.out.format("Enter the %d elements : ", Size);
		i = 0;
		while( i < Size) 
		{
			arr[i] = sc.nextInt();
			i++;
		}
		
		System.out.print("Please Enter the Array Item to Know = ");
		num = sc.nextInt();
		
		i = 0;
		while( i < arr.length) 
		{
			if(arr[i] == num)
			{
				occr++;
			}
			i++;
		}
		
		System.out.println(num + " Occurred " + occr + " Times.");
	}
}
Please Enter the size = 7
Enter the 7 elements : 22 9 22 7 54 22 1
Please Enter the Array Item to Know = 22
22 Occurred 3 Times.

This Java example uses the do while loop to count the occurrence of an element in a given array.

package NumPrograms;

import java.util.Scanner;

public class ArrayCountOccrence3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, num, occr = 0;
		
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter the Item to Know = ");
		num = sc.nextInt();
		
		System.out.print("Please Enter the size = ");
		Size = sc.nextInt();
		
		int[] arr = new int[Size];
		
		System.out.format("Enter the %d elements : ", Size);
		i = 0;
		do
		{
			arr[i] = sc.nextInt();
			if(arr[i] == num)
			{
				occr++;
			}

		} while(++i < Size);
		
		System.out.println(num + " Occurred " + occr + " Times.");
	}
}
Please Enter the Item to Know = 5
Please Enter the size = 9
Enter the 9 elements : 5 2 5 9 11 5 22 7 5
5 Occurred 4 Times.