• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop over array to check for duplicates

 
Karl Davies
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is driving me insane. I want to loop over the array and check that the random number that has been generated isn't already in the array before I insert it. I have to use an array. Here is the code:


Thank you for your time ! I hope you can help a confused newbie.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use a Set? By the way variables should start with an lower-case character.
 
Karl Davies
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you mean RandGen, so yep fixed that thank you . I have to use an array as its in the requirements, I really don't want to be spoon fed the answer I just want a hint in the right direction, thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how would you, as a human being, look at a bunch of numbers and see if there are any duplicates?

The first steps in writing code is writing it out in English (or whatever natural language you choose) One of the mods will tell you to get a pencil with a BIG eraser and some paper. don't write ANY code until you have all the steps worked out there first.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Davies wrote:



You are expecting the above code to do the checking if the random value generated is not in the list? But I dont see its checking in the entire array. Its just comparing the random number generated with one of the values of the array and not the complete array.

There are lot of algorithms around which you can use for searching. Basically you would have to do this::
1. Generate random number
2. Search for it in the array- Use some search algorithm.
3. Insert it at the end of the array.

I hope this is the right direction you were looking for
 
Karl Davies
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this is what I have so far with using binary search, but it is still isn't working because it doesn't seem to return the index element if it it does already occur in the array, it returns the negative values fine, but I need the indexes to start the while loop.

And thank you for all your help so far guys!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find this code very hard to read. The single method does a lot of things. A golden rule that I follow is that a method should only do one thing. I would
recommend you to split the method up into smaller methods. Then you'll get a much better overview of how the method works.

Also Arrays.binarySearch() only works on sorted arrays. Otherwise the outcome will be unpredictable.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Wouter. Use of methods with meaningful names will make it easier to read the code.
 
Karl Davies
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It confuses me to, haha. I'll change it. When I do try to sort it first thing in the while loop with "Arrays.sort(randArray);", it loses the first 25 elements in the array, so when I print
the contents out its:

000 000 000 000 000

000 000 000 000 000

000 000 000 000 000

000 000 000 000 000

000 000 000 000 000

002 041 043 044 125

128 172 190 210 237

307 308 334 340 360

475 555 604 615 660

717 751 757 840 963

Thanks for the advice.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reiterating my earlier advice (my first post)-

while the array is not filled
{
Step-1: Generate a random number
Step-2: Search for the number in the array
Step-3: If not found- Insert in such a way that the array is sorted after the insertion (may be you can use Insertion sort or other sorting algorithm)
}

You can set the exit condition of the loop to the size of the declared array.

So you have one outer loop which iterates till your array is filled. Then a search method which returns true or false (or you can use the API). Then a method which will insert the element in the array, keep the array sorted.

Hope am clearer now. If you can break up your code according to the steps, it will make the code easier to read.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic