Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Removing Duplicate Values From an Array

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    My Mood
    Grumpy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Removing Duplicate Values From an Array

    I am to remove all duplicates form an array.

    For example:
    4 7 11 4 9 5 11 7 3 5
    should be changed to
    4 7 11 9 4 3

    I am not supposed to use hashtables. Is there a simple way to do this. I am trying to do it with multiple for loops.

    Thanks for all your help.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Removing Duplicate Values From an Array

    The simplest way would be to use Sets but I assume you are not allowed to use them either.

    You could create a 3rd array and only add a value to it if it is not present in either of the first 2 arrays. Or you would need to loop over the smaller array and for each value loop over the larger array. When you find a match, move all values down one spot. This has 2 problems to overcome:

    You will still have values left at the end of the array eg after "removing" the first 4 the array would be 7 11 4 9 5 11 7 3 5 5, then after the next 4 it would be 7 11 9 5 11 7 3 5 5 5.

    The other problem would be if you had the same value next to each other: 4 4 3 5 1 7. If you are not careful then removing the first 4 gives 4 3 5 1 7 7. Then you continue on searching at the second element and leave the other 4 at the start of the array.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Duplicate Values From an Array

    Another idea. Use a contains() type method.
    Copy the first number to the new array.
    Then in a loop
    get the next number from the old array
    does the new array contain this number
    if not add it
    end loop

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Removing Duplicate Values From an Array

    Yeah that is what I meant by my first suggestion but I didn't word it very well. Thanks for the clarification Norm.
    Improving the world one idiot at a time!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Duplicate Values From an Array

    I left out the problem of array sizes.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Removing Duplicate Values From an Array

    That is why working with Collections is by far the better option. But when you are a poor 1st year student you get stuck with silly assignments doing a kludge with an array.
    Improving the world one idiot at a time!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing Duplicate Values From an Array

    I programmed assembly all my life. Arrays are much better.

Similar Threads

  1. Testing values in an array.
    By bholzer in forum Loops & Control Statements
    Replies: 7
    Last Post: September 28th, 2011, 05:18 PM
  2. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  3. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  4. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM

Tags for this Thread