Answers for "java find all instances of int in arry"

0

java find all instances of int in arry

// Java program to count occurrences 
// of an element 

class Main 
{ 
	// Returns number of times x occurs in arr[0..n-1] 
	static int countOccurrences(int arr[], int n, int x) 
	{ 
		int res = 0; 
		for (int i=0; i<n; i++) 
			if (x == arr[i]) 
			res++; 
		return res; 
	} 
	
	public static void main(String args[]) 
	{ 
		int arr[] = {1, 2, 2, 2, 2, 3, 4, 7 ,8 ,8 }; 
		int n = arr.length; 
		int x = 2; 
		System.out.println(countOccurrences(arr, n, x)); 
	} 
}
Posted by: Guest on March-15-2021

Code answers related to "java find all instances of int in arry"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language