I am trying to filter the duplicate based on a custom field. Its working fine but it is filtering only one duplicate. if there are 3 records, its showing 2 records. The below is my code.

List < NEX_System_Component__c > olstServerNodes = new List < NEX_System_Component__c > ();

set < string > nxset = new set < string > ();

for (NEX_System_Component__c nx: [select NEX_GUID__c From NEX_System_Component__c]) {

olstServerNodes = [Select Id, Name, NEX_GUID__c, NEX_Asset__c, NEX_License_File__c, NEX_Node_Name__c, NEX_Type__c, NEX_Quantity__c, NEX_System__c, NEX_System_Component_Number__c, NEX_System_Component_State__c, NEX_Unit_Of_Measure__c, NEX_Manufacturer__c, NEX_Mfg_Ref_Number__c, NEX_License_Key__c, NEX_Last_License_Generation_Date__c, NEX_License_Expiry_Date__c, Nex_System__r.NEX_Product__r.NEX_License_Type__c, Nex_System__r.NEX_Friendly_Name__c, Nex_System__r.NEX_System_Number__c, NEX_Product_Name__c from NEX_System_Component__c where NEX_System__c =: SystemID and NEX_Type__c = 'Server'
    and NEX_System_Component_State__c = 'Inactive'
    AND NEX_GUID__c =: nxset order by NEX_Node_Name__c nulls last, NEX_Type__c

system.debug('olstServerNodes ::' + olstServerNodes.size());

set < NEX_System_Component__c > myset1 = new set < NEX_System_Component__c > (olstServerNodes);

// myset1=[select NEX_GUID__c from NEX_System_Component__c];

list < NEX_System_Component__c > mylist1 = new list < NEX_System_Component__c > (myset1);

for (integer i = 0; i < mylist1.size(); i++) {
    NEX_System_Component__c fcci = mylist1.get(i);

    for (integer j = i + 1; j < mylist1.size(); j++) {

        NEX_System_Component__c fccj = mylist1.get(j);
        if (fcci.Nex_Guid__c.trim() == fccj.Nex_Guid__c.trim()) {
            system.debug('REMOVED::' + fcci.Nex_Guid__c);

//update mylist1;
system.debug('updated list::' + mylist1.size());

system.debug('========final list=====' + mylist1);

Please help on this


The reason why is here:

if(fcci.Nex_Guid__c.trim()== fccj.Nex_Guid__c.trim()) {

Presuming all three values show up consecutively, then you end up skipping one of the records because you did not adjust j to account for the record you just removed. To visualize this, you just need to lay out how this is currently working:

i  j   mylist1
0  1   id1 id1 id1 [0 == 1]
0  1   id1 <removed> id1
0  1   id1 id1
0  2   id1 id1 [j == mylist1.size()]

So, instead, you would want to adjust j accordingly:

if(fcci.Nex_Guid__c.trim()== fccj.Nex_Guid__c.trim()) {
    mylist1.remove(j--); // Decrement and try again

Other alternatives include counting backwards towards i:

for (integer j = mylist1.size() - 1; j > i; j--) {

Or, ideally, using a Map, as Tushar mentioned. In fact, using this design in a large loop may have serious performance implications; in a list of just 200 items, you're already having to make 20,000 comparisons, which can easily eat up several seconds of CPU time, versus a Map, which would keep it in the approximately 200-300ms range (at least 10x faster).

  • thanks @sfdcfox , I would like add one more criteria that if the field value is in active list query , this inactive query should not return the same value
    – Chinna
    Aug 27 '18 at 11:01

You can handle this easily using Map

Map<String, NEX_System_Component__c > obMap = new Map<String, NEX_System_Component__c >();

for(NEX_System_Component__c fcci : mylist1)
   if(!obMap.containsKey(fcci.Nex_Guid__c.trim()) )
         obMap.put(fcci.Nex_Guid__c.trim(), fcci); // you will get only unique records.

The issue with your current code is once you remove any element there order changes by -1 and you skip the very next record.


It seems you have written too much lines of code.

You are adding to the list first and then checking if the element is present, if yes you are removing it.

You can simply add the element to the set when u add to the list, and just check if the set already cotains the element before adding to the list. See sample code:

Set<String> uniquenxSet=new Set<String>();
List<NEX_System_Component__c> uniqueElementList =new List<NEX_System_Component__c>();
for(NEX_System_Component__c  nx: [Select Id,Name,NEX_GUID__c,NEX_Asset__c, NEX_License_File__c from NEX_System_Component__c where NEX_System__c =: SystemID and NEX_Type__c ='Server'
             and NEX_System_Component_State__c = 'Inactive' AND NEX_GUID__c =:nxset order by NEX_Node_Name__c nulls last, NEX_Type__c]{

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.