Results 1 to 4 of 4
  1. #1
    sarthakG is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default please help....as soon as posss.....array prob...

    how to remove duplicate elements from an array?????

    eg...input..{1,2,3,4,5,1,2,3,4,6,7}
    output......{1,2,3,4,5,6,7}

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Create a Set of somekind (HashSet perhaps), add all the elements to the Set. "Set" ensures there is only 1 copy of each unique instance.

    Uniqueness is determined via the "equals" method of the class in question. For Integer, "equals()" will return true for instances that have equal intValues().

  3. #3
    windie is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    Easiest method would be to create a new array and copy the contents of the old one into it 1 by 1. if an element is repeated, it is not added into the new array.

    if you don't want to create a new array, then u use a for-loop to search through the entire array for repeated elements and replace them with zero. then print all non-zero elements

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

    Default

    Quote Originally Posted by windie View Post
    Easiest method would be ....
    I politely disagree. toadaly's suggestion is much easier and cleaner than yours.

Similar Threads

  1. java prob...
    By palms_09@yahoo.com in forum New To Java
    Replies: 2
    Last Post: 01-09-2009, 04:09 PM
  2. for each prob
    By kusumathatavarthi in forum New To Java
    Replies: 7
    Last Post: 05-21-2008, 01:00 PM
  3. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 06:11 PM
  4. having prob with Exception
    By eva in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 05:44 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
  •