Java Program to count occurance of number
Program
import java.util.*;
public class CountOccurence
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of an array:");
int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter the array elements:");
for(int i = 0; i < size ; i++)
arr[i] = sc.nextInt();
System.out.println("Enter the item to check:");
int item = sc.nextInt();
int count = 0;
for(int i = 0; i < size ;i++)
if(arr[i] == item)
count++;
if(count %gt; 0)
System.out.println("Element found for " + count + " times");
else
System.out.println("Element not found");
}
}
Output 1:
Enter the size of an array:
10
Enter the array elements:
12
34
5656
23
78
12
45
12
678
32
Enter the item to check:
12
Element found for 3 times
Output 2:
Enter the size of an array:
6
Enter the array elements:
11
21
31
41
51
61
Enter the item to check:
71
Element not found