I have a requirement where I need to find duplicates and remove from the list, but I am unable to figure how to iterate and remove duplicates. Can anyone please help me with this? I don't want to use sets, my requirement is different so I want to process within the list.
public List<Schedules__c> planSchedule(Map<Id, Opportunity> OppsPriorID) {
List<Schedules__c> SchedulesToProcess = [
SELECT Opportunity__c,Month__c,Year__c, term__c
FROM Schedules__c
WHERE Opportunity__c IN : OppsPriorID.keySet()
];
/*the above list may contain duplicates that means for different records Month__c and Year__c values will be same
Record 1: Opportunity__c = 'Test',Month__c = 'Aug',Year__c = 2021, term__c = 1
Record 2: Opportunity__c = 'Test',Month__c = 'Aug',Year__c = 2021, term__c = 2
Record 3: Opportunity__c = 'Test',Month__c = 'Mar',Year__c = 2021, term__c = 1
Record 4: Opportunity__c = 'Test',Month__c = 'Sep',Year__c = 2021, term__c = 3
Since Month and Year are repeating,we are considering Record 1 and Record 2 are duplicates even term__c field is different
so when iterating list below I would like to remove Record 2*/
for (Schedules__c oppSchedule : SchedulesToProcess) {
oppSchedule.Opportunity__c = OppsPriorID.get(oppSchedule.Opportunity__c).Id;
}
return SchedulesToProcess;
}