This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to remove duplicate elements from arraylist without using hashset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to remove duplicate elements from arraylist without using hashset" Watch "How to remove duplicate elements from arraylist without using hashset" New topic
Author

How to remove duplicate elements from arraylist without using hashset

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73



I want to know remove duplicate without using hashset.
kindly suggest.
Andrea Binello
Ranch Hand

Joined: Oct 30, 2013
Posts: 31

upanshu vaid wrote: I want to know remove duplicate without using hashset.
kindly suggest.

If you don't want to (or can't for didactic reasons) use other collections, apart the original list and the new list, you have to do a linear search every time you are inserting an element in the new list.

You are inserting the first "upanshu" into arrayList2. Does it already exist in arrayList2? No, ok insert it.
You are inserting the second "upanshu" into arrayList2. Does it already exist in arrayList2? Yes, skip it.

You have to do a loop on l and for each element, a loop on arrayList2 to verify if the element is already present.


Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
That shows how not to insert duplicates.
Removing duplicates is similar: you would have to do a linear search. Maybe use the sublist method. You would also have to decide which elements to remove. Do you remove the first occurrence or subsequent occurrences?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8414
    
  23

upanshu vaid wrote: I want to know remove duplicate without using hashset.
kindly suggest.

I'm afraid that's a very broad question.

If your question is "how can I prevent duplicates on each addition", then the suggestions you've been given are probably the best. On the other hand, if your question is simply "how can I remove duplicates from my List" then I'd say thatis going to be about as fast as you can get.

However, you need to load ALL your values (including duplicates) into myList first.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 700
    
  11
Hmm... LinkedHashSet is dubious, given that HashSets are not allowed.
So I would advise to use a TreeSet instead.

Another method would be to sort the List, and remove every element that is not
element 0 and is equal to its predecessor.

Greetings,
Piet
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Is any predefined Set permissible? Sorting sounds a good idea, but are you allowed to lose the ordering information?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to remove duplicate elements from arraylist without using hashset