yeah absolutely, you should use a for loop for this, especially if the size of the array may vary
for (int i = 0; i < x.length; i++) {
for (int j = i+1; j < x.length; j++) {
if (x[i] == x[j]) {
//do something...
}
}
}
null
Do you need to know where the repeat is? Do you need to know what number is repeated? Or, just "Are all numbers unique?"
If the last, you could try dumping everything into a HashSet. You'd need to create Objects (Integer, Double, etc.) out of the values in the array, but it is the easiest way.
Set mySet = new HashSet();
// Put each element of the array into the Set separately, using a loop.
// (I assume it is an array of primitives. If they aren't primitives, you can make the Set in one line.)
if (mySet.size() == x.length)
{
System.out.println("There were no repeats.");
}
The Set's "add" method will tell you as you put each value in whether that element is already in the Set.
Ok, let me explain the problem a little bit better, I have to do a lottery ticket or something like that, so in one part of the program I need to create a method that does a QuickPick witht he Random generator, the problem is that I need to check that those random numbers do not repeat, so that's why I was checking each array with each other, I need a total of 6 picks so the list I put in here is obviously a bit short.
Random generator = new Random();
Set<Integer> numbers = new HashSet<Integer>();
while ( ... ) // ADD CODE THAT SAYS "size of set is too small"
{
bool added = numbers.add(Integer.valueOf(generator.nextInt(MAX_NUM));
System.out.println(added);
}
The bool added isn't needed above--it will just tell you if you added the value or if it was a duplicate.