function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishnu_SFDCVishnu_SFDC 

Iterate through the list and compare each element with every other element.

To achieve this i am using two for loops which is a bad approch. can any one suggest me a better approch than this.

for example: if i compare slot[10] and slot[11] and i remove slot[11], i also have to compare slot[10] and slot[12] which is not happening in below code without  using extra for loop.

T
for(integer j=0;j<=repsid.size();j++)
    { 
    for(integer i=0;i<=slots.size()-2;i++)
    {
        if(slots[i].StartDateTime==slots[i+1].StartDateTime) 
        {
            if(slots[i].NumberofEvents==slots[i+1].NumberofEvents && slots[i].distance==slots[i+1].distance)
            {
                slots.remove(i+1);
            }
            else
            {
                if(slots[i].NumberofEvents==slots[i+1].NumberofEvents && slots[i].distance!=slots[i+1].distance && apptmin != 90)
                {
                    if(slots[i].distance<slots[i+1].distance)
                    {
                        slots.remove(i+1);                  
                    }
                    else
                    {
                        slots.remove(i); 
                    }
                }
                else
                {
                    if((slots[i].NumberofEvents!=slots[i+1].NumberofEvents && slots[i].distance>10 && slots[i+1].distance>10) || slots[i].NumberofEvents!=slots[i+1].NumberofEvents && apptmin==90)
                    {
                        if(slots[i].NumberofEvents<slots[i+1].NumberofEvents)
                        {
                            slots.remove(i+1);
                        }
                        else
                        {
                            slots.remove(i);
                        }
                    }
                    else
                    {
                        if(slots[i].distance < slots[i+1].distance)
                        {
                            slots.remove(i+1);
                        }
                        else
                        {
                            slots.remove(i);

                        }
                    }
                }
            }
        }
        }
    }

hanks for your help.
Best Answer chosen by Vishnu_SFDC
Tony TannousTony Tannous
you can use the map object with the below format

map<date,list<slots>> mapSlot= new map<date,list<slots>>();

for(integer i=0;i<=slots.size()-2;i++)
  {
    if( !mapSlot.containskey(slots[i].StartDateTime))
      {
          list<slots> listSlots= new list<slots> ();
          mapSlot.put(slots[i].StartDateTime,listSlots);
     }
    

   VerifiyData (mapSlot,slots[i]);
}
// this method to put  your business logic inside.
private void VerifiyData( map<date,list<slots>> mapSlot ,slots slot1 )
{

   if(mapSlot.size() ==0)
  {
       mapSlot.get(slot1.StartDateTime).add(slot1);
   }
  else
  {
            list<slots> listSlots= mapSlot.get(slot1.StartDateTime);
             foreach (slots slt :listSlots)
             {
                       // put your logic here and verify slot1 with the listSlots and remove from the object listSlots the unwanted value
             }
}

}

and once you are done you will have a map that contains the correct data.

Regards.