Count Number of Occurrences

Get an array of Integers arr[] with size n and element e to be counted. Apply Linear Search / Binary Search to count the occurrence of element e in the array arr[]. Print the count at the end.

Examples

Input 1:

5

5 7 9 3 9

9

Output 1:

2

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution

				
				
					

import java.util.*;

class SearchCO

{

public static void main(String args[])

    {


//Variable and  Object Declarations

        Scanner input = new Scanner(System.in);

int n,e,count=0;

//Getting size of Array & Declare Array arr[]

n = input.nextInt();

int arr[] = new int[n];


//Get n values to array arr[]

for(int i = 0; i<n; i++)

{

arr[i] = input.nextInt();

}


e = input.nextInt();


//Linear Search

for(int i = 0; i<n; i++)

{

if(arr[i]==e)

{

count++;

}

}

System.out.print(count);

      }

}

Program Explanation

Comments