This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Searching through an array to find and print duplicate random numbers

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't work at all your solution is infinite loop in itself does any1 else have a solution to my problem???
 
Marshal
Posts: 74632
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declare i outside of the for loop

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 74632
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 74632
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, Brian. I was getting side-tracked.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic