Results 1 to 7 of 7
  1. #1
    'Rasta. is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Getting rid of duplicates in my Array problem

    Hi there,
    I'm relatively new to java and I have this one little problem which is probably quite simple, but I cant seem to get my head around it and solve it.
    What I am trying to do is eliminate duplicates in my array.
    Basically I want the user to be able to input 5 numbers,
    then I want it to print only the numbers that are not duplicates, and also
    the numbers that are less than 10 or greater than 100.

    Example:
    the user inputs: 10, 20, 10, 20, 101
    output: 10, 20, 101

    another example:
    the user inputs: 1, 99, 101, 235, 99
    output: 99 (because 1, 101 and 235 are outside of 10 and 100 and 99 is there twice)

    This is my code so far: It just detects which numbers are duplicates.. but I cant figure the rest out at all :(

    Java Code:
    import java.util.Scanner;
    
    public class Duplicate
    {
        public static void main(String[] args)
        {
            // instance variables - replace the example below with your own
            Scanner input = new Scanner(System.in);
            int[] myArray; //declare array
            myArray = new int[5]; // 5 int - 0,1,2,3,4
            
            //int x = 0;
            
            int y = 1;
            
            
            System.out.print("\nPlease input five numbers between 10 and 100.\n");        
            
                  
            
            
            for(int i=0; i<5; i++)//loop 5 times
            {
                myArray[i] = input.nextInt();//input the 5 values
                /*
                if(Array[i]<10 || Array[i]>100)//if its between 10 and 100 continue else return.
                {
                    Array[i] = 0;
                }
                */
            }
            System.out.println("Output");
            for (int i = 0; i <= myArray.length - 1; i++) 
            {
                for (int j = i + 1; j <= myArray.length - 1; j++) 
                {
                    if (myArray[i] != myArray[j]) 
                    {
                        System.out.printf("\n%d", myArray[i]);
                        
                    }
                    else
                    {
                        return;
                    }
                    
                    
                }
            }
    Sorry that its a bit messy, but I cleaned up most of it(was really messy before). I think up until the line
    Java Code:
    System.out.println("Output");
    everything is right, from there Im completely lost

    Help would be greatly appreciated :D

  2. #2
    'Rasta. is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    I just randomly got a burst of inspiration and figured out how to do it.
    However I'm not 100% happy with it because its a bit long and repetitive.
    If you could help me shorten this it would be amazing!!
    Java Code:
    import java.util.Scanner;
    
    public class Duplicate
    {
        public static void main(String[] args)
        {
            // instance variables - replace the example below with your own
            Scanner input = new Scanner(System.in);
            int[] Array; //declare array
            Array = new int[5]; // 5 int - 0,1,2,3,4
            
            
            
            
            System.out.print("Please input five numbers between 10 and 100.\n");        
            
                  
            
            
            for(int i=0; i<5; i++)//loop 5 times
            {
                Array[i] = input.nextInt();//input the 5 values
            }
    
            System.out.print("Output:");
    
            if(Array[0]>=10 && Array[0]<=100)
            {
                System.out.printf("\n%d", Array[0]);
            }
            
            if(Array[1]>=10 && Array[1]<=100)
            {
                if(Array[1]!=Array[0])
                {
                    System.out.printf("\n%d", Array[1]);
                }
            }
    
            if(Array[2]>=10 && Array[2]<=100)
            {
                if(Array[2]!=Array[0] && Array[2]!=Array[1])
                {
                    System.out.printf("\n%d", Array[2]);
                }
            }
            
            if(Array[3]>=10 && Array[3]<=100)
            {
                if(Array[3]!=Array[0] && Array[3]!=Array[1] && Array[3]!=Array[2])
                {
                    System.out.printf("\n%d", Array[3]);
                }
            }
            
            
            if(Array[4]>=10 && Array[4]<=100)
            {
                if(Array[4]!=Array[0] && Array[4]!=Array[1] && Array[4]!=Array[2] && Array[4]!=Array[3])
                {
                    System.out.printf("\n%d", Array[4]);
                }
            }
            
            System.out.println("\nWin!!\n");
        }
    }
    Thanks in advance!
    Rasta

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by 'Rasta. View Post
    If you could help me shorten this it would be amazing!!
    are you forced to use arrays? :D

  5. #5
    'Rasta. is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes, cause I haven't done much else in java and I would love to get the 'perfect' code because I've been working on it like mad for 5-6 days lol

  6. #6
    'Rasta. is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Think nested for loops. :)
    I have no idea what you mean with that :confused:

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Think of how you'd do it on paper without a computer. One way is to go forward through the array looking to see if the current array item and has duplicates before it, and if not, print it.

    So one loop will go forward, looking at each array item, and the inner loop will go backwards from the current item - 1 down to 0 looking for duplicates..... :)

Similar Threads

  1. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM
  2. Duplicates in String Array
    By turnergirl24 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 11:09 PM
  3. FInd the no. of duplicates in an array
    By singularity in forum Advanced Java
    Replies: 3
    Last Post: 09-04-2009, 09:25 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM

Posting Permissions

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