-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicateints.java
More file actions
27 lines (23 loc) · 887 Bytes
/
duplicateints.java
File metadata and controls
27 lines (23 loc) · 887 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
import java.util.Arrays;
public class duplicateints {
public static void main(String[] args) {
int[] a = {1,2,3,3,5,6,7,7,9,10};
System.out.println("Array: " + Arrays.toString(a));
System.out.print("Duplicates: ");
for (int i = 0; i < a.length; i++) {
// skip if we already handled this value before
boolean alreadyHandled = false;
for (int k = 0; k < i; k++) {
if (a[k] == a[i]) { alreadyHandled = true; break; }
}
if (alreadyHandled) continue;
// check if it appears again later
for (int j = i + 1; j < a.length; j++) {
if (a[i] == a[j]) {
System.out.println(a[i] + " is a duplicate value in the array");
break;
}
}
}
}
}