0

I create an array that then prints in sets of five. I want to then be able to search the array by 5 to see if there are any duplicates. I've tried but I can only think of a way to search by each value not five. If anyone can point me in the right direction, that would be great. Thanks.

 public class findPat {
        static int arr [] = new int [10];
        static int st = 1;
        static int end = 56;
        static double t1;
        static double t2;

        public static void main(String[] args){

            t1=System.currentTimeMillis(); 
            for(int n=0; n<100; n++){   
                for (int i=0; i<arr.length; i++)
                    arr[i]= (int) (Math.random()* (end-st +1)) +st;

                for (int i=0; i<5; i++){ 

                    if (i%5==0) 
                        System.out.println(); 
                        System.out.print("\t" + arr[i]);} 
                    }
            t2=System.currentTimeMillis();
            System.out.println();
            System.out.println();
            System.out.println("\t" + "Total run time is " + ((t2-t1)) + "ms");

            }
        }

the console looks like this:

    18  22  42  14  38
    2   2   14  9   8
    6   29  38  37  33
    6   41  41  27  7
    20  41  38  11  50
    16  17  41  21  19
    40  33  9   10  7
    12  54  10  30  36

however each row is in the same array but is just printing 5 at a time. the console will have more than just those few lines. I want to be able to search the array and check each row against the rest to see how many times it appears, if it does.

Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59
  • What do you mean "search by 5"? – Graham Borland Mar 27 '12 at 16:23
  • I don't understand the question. please provide a sample input and a sample output that clarifies the problem. – amit Mar 27 '12 at 16:23
  • there are many ways, look this thread: http://stackoverflow.com/questions/3951547/java-array-finding-duplicates – Oscar Castiblanco Mar 27 '12 at 16:29
  • I edited the question, maybe it makes more sense. – Eric Goncalves Mar 27 '12 at 16:32
  • At the moment your code prints the first 5 random numbers out of an array of ten random numbers. It repeats this 100 times. Your question suggests that you want to search for duplicate series of 5 values. Would it be possible to provide a more clear idea of the task you want to accomplish or a code example that more clearly shows what you are trying to do? – len Mar 27 '12 at 16:37
  • Question smells of homework. Is this homework/some kind of lab exercise? If it is, can you please tag it as homework? – blahman Mar 27 '12 at 16:39
  • I wil try to explain in more detail.it should be. static int arr [] = new int [5]; for example, I want to search the array for the first 5 values in the array then if there is a match, matchCtr++; and the program continues until all the whole array is searched against the first row. then it searches the second row against the rest the array, and repeats. – Eric Goncalves Mar 27 '12 at 16:42

1 Answers1

1

You could implement this using a Hashtable. Using your code as a base, I've written an example implementation but without knowing what it is you are trying to do, I can't judge if this is what you are looking for.

import java.util.Hashtable;

public class findPat {
    static final int COUNT = 100;

    static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();
    static String groupInteger = "";
    static int arr [] = new int [5];
    static int st = 1;
    static int end = 56;
    static double t1;
    static double t2;

    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] = (int) (Math.random()* (end - st + 1)) + st;

            }
            for (int i = 1; i <= 5; i++) {
                groupInteger += arr[i-1];
                System.out.print("\t" + arr[i-1]);
                if (i % 5 == 0) {
                    System.out.println();
                    if (compareSet.containsKey(groupInteger)) {
                        System.out.println("duplicate found");
                        int currentCount = compareSet.get(groupInteger);
                        compareSet.put(groupInteger, currentCount + 1);
                    } else {
                        compareSet.put(groupInteger, 1);                        
                    }
                    groupInteger = "";
                }

            } 
        }
        t2=System.currentTimeMillis();
        System.out.println();
        System.out.println();
        System.out.println("\t" + "Total run time is " + ((t2 - t1)) + "ms");
    }
}

This code keeps track of the unique sets of random numbers by adding them (creating a key value that is the same for every set that has the same values in the same order, the concatenated string takes care of this).

Your code ran in 13 seconds on my system, mine takes 17 seconds. Now if runtime is of crucial importance, you might want to look into hashing techniques. But I'm not sure if you will be able to shave off a lot as you will have to add some extra code which will take extra time.

len
  • 335
  • 1
  • 5
  • This is not 100% but it does put me in the right direction, Thank you very much. – Eric Goncalves Mar 27 '12 at 17:35
  • I ran the program but, when i print compareSet to see which values are duplicated i get this {3623955=1, 311471043=1, 191644650=1, 2754351=1, 34492846=1, 431540317=1, 5=1, 3649244028=1, 748465055=1, 1141473319=1} but the numbers don't match up to what is in the array, it starts from an arbitrary spot in the array. – Eric Goncalves Mar 27 '12 at 17:59
  • sorry, my mistake, I assumed your code prints after every fifth item in the array but due to i%5 being 0 only when a number is dividable by 5 and the fifth element having index 4, it stores the first element followed by sets of elements 5 positions further (2nd-6th, 7ht-11th, ...). I've edited my code. – len Mar 27 '12 at 19:17