-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_element_occurrence.java
More file actions
58 lines (45 loc) · 1.89 KB
/
Copy pathcount_element_occurrence.java
File metadata and controls
58 lines (45 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//How many times a given number appear?
import java.util.Scanner;
//class contains array and element occurrence finding logic
class ArrayIndex{
int[] arr; //instance variable holds the value
int n; //size of an array
//Method to read array size and elements from the user
void inputArray(Scanner sc){
System.out.println("Enter the size of an array: ");
n = sc.nextInt(); //Read the size of an array
arr = new int[n]; //Initialize an array
System.out.println("Enter " + n + " elements");
for(int i=0; i<n; i++){
arr[i] = sc.nextInt(); //Read each element in an array
}
}
//Method to count the same element occurred in an array
public int countOccurrence(int key){
int count = 0;
for(int i=0 ; i<n ; i++){ //Traverse the array
if(arr[i] == key){ //Check if the current element matches the key
count++; //If matches increase the count
}
}
return count; //Read the count
}
}
public class count_element_occurrence{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //Scanner object for the input
ArrayIndex obj = new ArrayIndex(); //Create an object for ArrayIndex
obj.inputArray(sc); //Read array elements from the user
System.out.println("Enter the element to search: ");
int key = sc.nextInt(); //Read element to search
int count = obj.countOccurrence(key); //Count the appearance of the same element
//Display result
if(count > 0){
System.out.println(key + " appears " + count + " times in an array" );
}
else{
System.out.println(key + " element not found in an array");
}
sc.close(); //Close the Scanner to prevent resource leak
}
}