forked from Nikhil-2002/Programming_Hactoberfest23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindRepeatingElements
30 lines (26 loc) · 882 Bytes
/
FindRepeatingElements
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
import java.util.HashSet;
public class FindRepeatingElements {
public static void findRepeatingElements(int[] arr) {
HashSet<Integer> set = new HashSet<>();
HashSet<Integer> duplicates = new HashSet<>();
for (int num : arr) {
if (!set.add(num)) {
// If 'num' is already in the 'set', it's a duplicate.
duplicates.add(num);
}
}
if (duplicates.isEmpty()) {
System.out.println("No repeating elements found.");
} else {
System.out.print("Repeating elements: ");
for (int num : duplicates) {
System.out.print(num + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 5, 4, 6, 7};
findRepeatingElements(arr);
}
}