Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java Help :O

Name: ipoopmypants !49.zlWoq9M 2009-11-24 22:53

Anyone know how in Java I check check an array to see if there are any duplicates?

        int[] random = new int[100];
        Random randomGenerator = new Random();
        boolean inArray = true;

for(int i = 0; i < random.length; i++)
        {
            random[i] = randomGenerator.nextInt(999);
            if(random[i] == /*CHECK ALL OTHER INDEXES IN THE ARRAY*/)
            {
                random[i]++;
            }
           
            }

Name: Anonymous 2009-11-24 23:29

I think I understand your problem, but the implementation above is confusing.  Why are you generating a random number?

int[] random = new int[100];
boolean unique = true;
int i = 0, j = 0, k = random.length;
for( ; i < k; i++)
{
   for(j = i + 1; j < k; j++)
   {
      if(random[i] == random[j])
      {
         unique = false;
         // do something?
      }
   }
}

It's not speed optimal as k gets larger but this may be the most straightforward way to do it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List