We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b87b0da commit ea57f16Copy full SHA for ea57f16
challenge27.java
@@ -0,0 +1,29 @@
1
+import java.util.Scanner;
2
+
3
+// Create a program to find number of occurrences of an element in an array.🚀
4
+public class challenge27 {
5
+ public static void main(String[] args) {
6
+ Scanner scanner = new Scanner(System.in);
7
+ System.out.println("Enter the size of the array:");
8
+ int n = scanner.nextInt();
9
+ int[] arr = new int[n];
10
+ System.out.println("Enter the elements of the array:");
11
+ for (int i = 0; i < n; i++) {
12
+ arr[i] = scanner.nextInt();
13
+ }
14
+ System.out.println("Enter the element to count:");
15
+ int x = scanner.nextInt();
16
+ int count = countOccurrences(arr, x);
17
+ System.out.println("The number of occurrences of " + x + " in the array is: " + count);
18
19
20
+ public static int countOccurrences(int[] arr, int x) {
21
+ int count = 0;
22
+ for (int i = 0; i < arr.length; i++) {
23
+ if (arr[i] == x) {
24
+ count++;
25
26
27
+ return count;
28
29
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments