+ Start a Discussion
SennahSennah 

Remove duplicate items from a List

Hi Community,

 

who can provide a good and effective way to remove duplicates from a List of sObjects?

 

Of course without another SOQL ;)

 

Cheers,

//Hannes

Best Answer chosen by Admin (Salesforce Developers) 
rungerrunger

I can name that tune in 4 notes!

 

 

Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset);

 

Here's a good trick.  Sets, by definition, contain no duplicates.  So, the first addAll() causes any dupes to overwrite themselves when going into the set.  The second addAll() just gets you back to a list.  If you don't need the result to be a list, you can omit that.

 

Best part: you only get dinged for 4 statements in the governor limits.

 

Rich

All Answers

gv007gv007

what you are going to remove from list of objects.

SennahSennah

duplicates. :D

rungerrunger

I can name that tune in 4 notes!

 

 

Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset);

 

Here's a good trick.  Sets, by definition, contain no duplicates.  So, the first addAll() causes any dupes to overwrite themselves when going into the set.  The second addAll() just gets you back to a list.  If you don't need the result to be a list, you can omit that.

 

Best part: you only get dinged for 4 statements in the governor limits.

 

Rich

This was selected as the best answer
Greg RohmanGreg Rohman

Hello,

 

I know this is an old topic, but I've used that same technique to eliminate duplicates. The problem I've run into, though, is that Sets are unordered. So, when converted to a Set and then back to a List using addAll(), the order doesn't remain. Is there an efficient technique to remove duplicates while maintaining List order?

 

Thanks.

 

-Greg

rungerrunger

To maintain order, you need to iterate over the elements:

 

 

Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
for (sobject s : originalList) {
  if (myset.add(s)) {
    result.add(s);
  }
}

 Set.add() returns a Boolean, which is true if it was successfully added to the set.  If the object was already in the set, it will be false.

 

Greg RohmanGreg Rohman

Hi Rich.

 

That worked great. Thank you!

 

-Greg

red26782red26782

Works great...

RonGongRonGong

Found another way to acheive this and thought I'd share.  Use a map instead of a set.

 

Map<Id, SObject> mapSObj = new Map<Id, SObject>();
mapSObj.putAll(originalList);

 

Then use the map values as your list.  

 

-PM

frederikofrederiko
DuplicateFilesDeleter is a simple, but effective tool to locate duplicate files in one or more selected search paths. It scans the files and compares them based on Byte for Byte Comparison, which ensures 100% accuracy. You can then choose to delete the selected duplicate or original files. The program is multi-threaded and performs scans quickly.
Shaun Van WeeldenShaun Van Weelden

Without adding a lot of code to read through, you can do something as easy as this:

List<String> noDupes = new List<String>(new Set<String>(listOfDuplicates))

Lists and Sets can take each other as inputs to their constructors
Carrie Hooper 10Carrie Hooper 10
Above solution worked for me.