-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccurrencesArray.java
More file actions
76 lines (64 loc) · 2.04 KB
/
OccurrencesArray.java
File metadata and controls
76 lines (64 loc) · 2.04 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// public class OccurrencesArray {
// public static int noOfoccurences(int[] arr, int value) {
// int count = 0;
// for (int num : arr) {
// if (num == value) {
// count++;
// }
// }
// return count;
// }
// }
// import java.util.Scanner;
// public class OccurrencesArray {
// public static void main(String[] args) {
// try (Scanner input = new Scanner(System.in)) {
// System.out.println("Welcome to Array Occurrences\n");
// System.out.print("Enter the number of elements: ");
// int size = input.nextInt();
// int[] numArr = new int[size];
// for (int i = 0; i < size; i++) {
// System.out.print("Please enter element " + (i + 1) + ": ");
// numArr[i] = input.nextInt();
// }
// System.out.print("Now enter the number you want to find: ");
// int num = input.nextInt();
// int occurrences = noOfOccurrences(numArr, num);
// System.out.println("Your element was found " + occurrences + " times in the
// array.");
// }
// }
// public static int noOfOccurrences(int[] numArr, int num) {
// int occ = 0;
// for (int j : numArr) {
// if (j == num) {
// occ++;
// }
// }
// return occ;
// }
// }
import java.util.Scanner;
public class OccurrencesArray {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("Welcome to Array Occurences\n");
int[] numArr = ArrayUtility.inputArray();
System.out.print("Now enter the number you want to find: ");
int num = input.nextInt();
int occurrences = noOfoccurences(numArr, num);
System.out.println("Your element was found " + occurrences + " times in the array");
}
}
public static int noOfoccurences(int[] numArr, int num) {
int occ = 0;
int i = 0;
while (i < numArr.length) {
if (numArr[i] == num) {
occ++;
}
i++;
}
return occ;
}
}