Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea57f16

Browse files
🍵
1 parent b87b0da commit ea57f16

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎challenge27.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /