Possible Duplicate:
How to find a duplicate of length 5 in a single array. Java
I am trying to see if there are any duplicates in the array.
import java.util.Hashtable;
public class test {
static final int COUNT = 10;
static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();
static String groupInteger = "";
static long arr[] = new long[5];
static int st = 1;
static int end = 56;
static double t1;
static double t2;
static int ctr = 0;
public static void main(String[] args) {
t1 = System.currentTimeMillis();
for (int n = 0; n < COUNT; n++) {
for (int i = 0; i < arr.length; i++) {
arr[i] = (long) ((Math.random() * (end - st + 1)) + st);
}
for (int i = 0; i < 5; i++) {
groupInteger += arr[i];
if (i % 5 == 0) {
System.out.println();
if (compareSet.containsKey(groupInteger)) {
ctr++;
System.out.println("duplicate found ");
int currentCount = compareSet.get(groupInteger);
compareSet.put(groupInteger, currentCount + 1);
} else {
compareSet.put(groupInteger, 1);
}
groupInteger = "";
}
System.out.print(arr[i]);
}
}
t2 = System.currentTimeMillis();
System.out.println();
System.out.println();
System.out.println("\t" + "Total run time is " + ((t2 - t1)) + "ms");
System.out.println(compareSet);
}
}
the console:
12 23 8 44 23
28 13 39 49 53
37 40 16 53 48
6 45 14 52 20
32 4 41 10 38
38 29 25 21 13
16 34 45 26 11
22 33 54 21 10
40 34 53 37 50
20 26 48 32 51
I want to be able to check if there are duplicate rows.
I cant seem to finagle this code around to work properly.
Please let me know if you see something! Thank you
You are using inappropriate classes for that, if you want to find “any” duplicates
Use HashSet<String>
And your code is totally buggy. The if-then part must be after the for, without if.
And if you want to “count” duplicates, I would use HashMap