forked from 14visheshjain/DataStructure-Algorithm-in-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathallOccurInArray.java
More file actions
49 lines (38 loc) · 898 Bytes
/
allOccurInArray.java
File metadata and controls
49 lines (38 loc) · 898 Bytes
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
package lecture7;
import java.util.Scanner;
public class allOccurInArray {
static Scanner a = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = takeinput();
int n = a.nextInt();
display(allOccur(arr, 0, n, 0));
}
public static int[] takeinput() {
int[] array;
System.out.println(" enter size ");
int n = a.nextInt();
array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = a.nextInt();
}
return array;
}
public static void display(int[] ar) {
for (int val : ar) {
System.out.print(val+" ");
}
}
public static int[] allOccur(int[] arr, int vidx, int n, int count) {
if (vidx == arr.length) {
int[] newarr = new int[count];
return newarr;
}
if (arr[vidx] == n)
count++;
int[] newarr = allOccur(arr, vidx + 1, n, count);
if (arr[vidx] == n) {
newarr[count - 1] = vidx;
}
return newarr;
}
}