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.
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.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.