I have two lists mainGroupLinkerIPGlist and toCopy. i want to compare these two lists and remove the duplicates from them. for this i have created a set method and added both the lists to the Uniqueset method.but the set is combining both the lists instead of removing the duplicates.kindly let me know how to remove the duplicates?

public PageReference save() {

     List<Intralinks_Group_and_Contact_Links__c> toAdd = new List<Intralinks_Group_and_Contact_Links__c>();

     List<Intralinks_Group_and_Contact_Links__c> mainGroupLinkerIPGlist = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :mainGroupLinker.Contact__c];
        system.debug('mainGroupLinkerIPGlist =' + mainGroupLinkerIPGlist.size());

    //query all the Groups the copyFrom contact has
    if(copyFrom.Contact__c != null) {
       List<Intralinks_Group_and_Contact_Links__c> toCopy = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :copyFrom.Contact__c AND Id NOT IN :mainGroupLinkerIPGlist];
          system.debug('toCopy =' + toCopy.size());

          set <Intralinks_Group_and_Contact_Links__c> Uniqueset = New set<Intralinks_Group_and_Contact_Links__c>();
          system.debug('Uniqueset =' + Uniqueset.size());

         for(Intralinks_Group_and_Contact_Links__c copyGroup : toCopy) {

            Intralinks_Group_and_Contact_Links__c newCopy = new Intralinks_Group_and_Contact_Links__c();
            newCopy.Contact__c= mainGroupLinker.Contact__c;
            //system.debug('mainGroupLinker.Contact Id=' + mainGroupLinker.Contact__c);

            newCopy.Intralinks_Portal_Group__c= copyGroup.Intralinks_Portal_Group__c;

           //system.debug('copyGroup size=' + copyGroup.Intralinks_Portal_Group__c);  

             insert newCopy; 


  • still you are getting duplicate value in toCopy list? – Tushar Sharma May 31 '16 at 9:42
  • yes i am getting the duplicate values – chaitanya May 31 '16 at 11:17

You are getting duplicate values because in both the queries you are using different projections;

List mainGroupLinkerIPGlist = [SELECT Id,Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :mainGroupLinker.Contact__c];


List toCopy = [SELECT Intralinks_Portal_Group__c FROM Intralinks_Group_and_Contact_Links__c where Contact__c = :copyFrom.Contact__c];

As hashCode of an object depends on values, so for the same object in two list you will get two different hashcode.

So if you can fetch same columns in both the queries then your set will give you the unique items as result.


List<Account> acc1 = [select Id,name from Account order by createdDate limit 5];
List<Account> acc2 = [select Id from Account order by createdDate limit 6];
List<Account> acc3 = [select Id,name from Account order by createdDate limit 6];

Set<Account> accSet1 = new Set<Account>(); accSet1.addAll(acc1); accSet1.addAll(acc2); system.assertEquals(11,accSet1.size());

Set<Account> accSet2 = new Set<Account>(); accSet2.addAll(acc1); accSet2.addAll(acc3); system.assertEquals(6,accSet2.size());


If you want to remove duplicates from both lists, you can make them sets(which will remove duplicates in them), create a new list where you will add all duplicates and then use removeAll method on the original sets.

An example:

List<Account> yourList1 = //...
List<Account> yourList2 = //...
Set<Account> yourSet1 = new Set<Account>(yourList1);
Set<Account> yourSet2 = new Set<Account>(yourList2);
List<Account> dupes = new List<Account>();
for (Account a1 : yourSet1) {
    for (Account a2 : yourSet2) {
        if (a1 == a2) {

This will remove all duplicate record from both of your lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.