-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindRepeatingElement.java
More file actions
50 lines (39 loc) · 1.34 KB
/
Copy pathFindRepeatingElement.java
File metadata and controls
50 lines (39 loc) · 1.34 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
package prepare;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class FindRepeatingElement {
public static void main(String[] args) {
System.out.println("Enter the size of the array: ");
try (Scanner sc = new Scanner(System.in)) {
int size = sc.nextInt();
System.out.println("Enter the elements of the array: ");
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println(Arrays.toString(arr));
System.out.println("Repeating elements are: ");
printRepeatingElements(arr);
}
}
public static void printRepeatingElements(int[] arr) {
int[] duplicateArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i != j && arr[i] == arr[j]) {
duplicateArr[i] = arr[i];
}
}
}
int[] newArr = new int[duplicateArr.length];
newArr = duplicateArr;
Arrays.sort(newArr);
HashSet<Integer> set = new HashSet<>();
for (Integer integer : newArr) {
set.add(integer);
}
set.remove(0);
System.out.println(set.toString());
}
}