Results 1 to 4 of 4
  1. #1
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Replace duplicates

    i want to replace all my duplicates without using hashset or shuffle
    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Rander
      {
     
        public static void main(String[] args) {
         int n= 0;
         int l = 1;
         int[] fifty = new int[50];
          while (n <50){
          Random rand = new Random();
          int  randNo = rand.nextInt(50) + 1; {
            }
            {
              if(randNo==(n))
              break;}
            
             fifty[n] = randNo;
             System.out.printf("%03d", randNo);
             System.out.print(" ");    
                  if(l%5 == 0)
                   System.out.println();   
            n++;
            l++;
            }
        }
    }
    Last edited by dibs; 12-08-2015 at 08:19 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Replace duplicates

    I assume you meant 'sort' instead of 'shuffle.' Or perhaps you meant you didn't want to write a shuffle routine. It looks like you really want to catch duplicates, not replace them. So check the array to see if you already have the number. If so, store it, if not, generate another.

    You also have some unneeded brace pairs in your code. And an ArrayList import which you don't use.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    dibs is offline Member
    Join Date
    Dec 2015
    Posts
    22
    Rep Power
    0

    Default Re: Replace duplicates

    i want to find all the duplicates and then replace them

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Replace duplicates

    Please continue on first post on this topic. Don't start multiple threads on the same problem.

    Thread closed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. replace chars without .replace
    By Badprogrammer in forum New To Java
    Replies: 2
    Last Post: 09-16-2014, 06:22 AM
  2. String.replace ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 03:04 AM
  3. Char replace
    By susieferrari in forum New To Java
    Replies: 4
    Last Post: 06-08-2011, 02:06 PM
  4. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •