aspose file tools*
The moose likes Beginning Java and the fly likes Searching through an array to find and print duplicate random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching through an array to find and print duplicate random numbers" Watch "Searching through an array to find and print duplicate random numbers" New topic
Author

Searching through an array to find and print duplicate random numbers

Timothy Scott
Greenhorn

Joined: Apr 23, 2009
Posts: 7

With the current code setup the print statement which states which numbers were duplicates is looped within the 1000 array? And when I bring it outside the first set of brackets it says its unable to find the variable i so how do I get that fixed? Any recommendations on how to remove that outside the loop but still have it work. The problem is when it finds a duplicate it needs to say 45 was found as a duplicate 2 times or however many its found

Scott Stromberg
Greenhorn

Joined: May 05, 2009
Posts: 2
Timothy Scott wrote:
With the current code setup the print statement which states which numbers were duplicates is looped within the 1000 array? And when I bring it outside the first set of brackets it says its unable to find the variable i so how do I get that fixed? Any recommendations on how to remove that outside the loop but still have it work. The problem is when it finds a duplicate it needs to say 45 was found as a duplicate 2 times or however many its found




I've simplified it for you here. It may not be the most efficient solution, but it's a quick-fix. In yours, you are populating your array 1000 different times (and checking for duplicates 1000 times), so it will loop for a very, very long time.

Timothy Scott
Greenhorn

Joined: Apr 23, 2009
Posts: 7
This doesn't work at all your solution is infinite loop in itself does any1 else have a solution to my problem???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Please don't write "any1": read this FAQ.

Have you considered a Set? If you create a Set and add the numbers in turn, if the add method returns false it was a duplicate.

And never write == false. Use the ! operator instead.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3566
    
  15
Declare i outside of the for loop


Joanne
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
The easiest way to solve this problem is to follow Campbell's idea and create a Set (lets call A) that you will add the numbers to. If the add method returns false then you can add the number to a different set (lets call B). If adding to set B was successful then you will set it's number of repeats to 1 otherwise if it's false you will increase it. Here's a quick example: (Psudo code)



Keep in mind this was written in psudo code and I left the logic out for counting the number of elements over 500, I'll let you fill in the blanks. Hope that helps though.


SCJA
~Currently preparing for SCJP6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Wouldn't you use a Map for the number of repeats? Use the String as the Key and an Integer as the Value; if you already have the Key, then you increase the Value by 1.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Campbell, since this was a question on arrays and we were stearing him towards using a Set I figured I'd stick with just Sets instead of a Set and a Map, but yes you would definately use a Map for the duplicates. This way works as well though ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Good point, Brian. I was getting side-tracked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching through an array to find and print duplicate random numbers