aspose file tools*
The moose likes Beginning Java and the fly likes loop over array to check for duplicates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loop over array to check for duplicates" Watch "loop over array to check for duplicates" New topic
Author

loop over array to check for duplicates

Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
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

Joined: Oct 25, 2008
Posts: 2700

Why don't you use a Set? By the way variables should start with an lower-case character.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
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

Joined: Oct 02, 2003
Posts: 11229
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Mohamed Sanaulla | My Blog
Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

Agree with Wouter. Use of methods with meaningful names will make it easier to read the code.
Karl Davies
Greenhorn

Joined: Sep 21, 2010
Posts: 6
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loop over array to check for duplicates