-1

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

Community
  • 1
  • 1
Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59
  • Well there are no duplicate rows in your output are there? – talnicolas Mar 27 '12 at 18:50
  • do you at least understand the code he gave you? http://stackoverflow.com/questions/9893580/how-to-find-a-duplicate-of-length-5-in-a-single-array-java – talnicolas Mar 27 '12 at 18:53
  • i understand, the array is generated randomly with numbers between 1-56 every time i run the program i get different values. and i'm printing close to 100,000 thousand rows. I want to be able to tell how many times there is a duplicate. – Eric Goncalves Mar 27 '12 at 18:54
  • but have you tested with a simple test that if there are two duplicate rows they are found or not? – talnicolas Mar 27 '12 at 18:55
  • it finds duplicates, but when i print compareSet, the arrays are off; for example, {1917454822=1, 48326949=1, 502272218=1, 61612212=1, 72812428=1, 5244323532=1, 2=1, 5521371113=1, 567311247=1, 41436725=1} – Eric Goncalves Mar 27 '12 at 18:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9377/discussion-between-eric-and-talnicolas) – Eric Goncalves Mar 27 '12 at 19:01
  • Easiest way is once you populate the elements write it out to a string or an array and parse element by element by adding it to hashmap/hashset. makes sense? – Venki Mar 27 '12 at 19:05
  • no, it doesnt haha. Ive never used or heard of hashmap or hashset. the hash part of the code was recommended by someone else on stackoverflow. – Eric Goncalves Mar 27 '12 at 19:08
  • Please refrain from reposting your questions. If you have details to add, edit the original question instead. – NullUserException Mar 30 '12 at 01:11

1 Answers1

1

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.

HashSet<String> compareSet = new HashSet<String> ();
...
groupInteger.empty ();
for (int i = 0; i < 5; i++) {
  groupInteger += arr[i] + " ";
}
System.out.println(groupInteger);
if (compareSet.contains(groupInteger)) {
    System.out.println("duplicate found ");
} else {
    compareSet.add(groupInteger);
}

And if you want to "count" duplicates, I would use HashMap

HashMap<String, Integer> compareSet = new HashMapt<String, Integer> ();
...
Integer act = compareSet.get(groupInteger);
if (act != null) {
    System.out.println("duplicate found ");
    compareSet.put(groupInteger, act + 1);
} else {
    compareSet.put(groupInteger, 1);
}
stefan bachert
  • 9,413
  • 4
  • 33
  • 40