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
RuchikaRuchika 

display the duplicate values present in a list.

Hi All. 

I have a list called allCertNo which is a list obtained from a csv. The list contains some tandom ten digit numbers called certnumbers. I want to check for duplicate certnumbers in this list and display the duplicate ones. checkDuplicates is a set.

 

List<String> temp = new List<String>();

Set<String> checkDuplicates = new Set<String>();

 

for (String s : allCertNo ){
boolean checkVal = checkDuplicates.add(s);
System.debug('checkVal is>>'+checkVal );
if(!checkVal) {
temp.add(s);
numStatus = false;
countErrors++;
System.debug('Duplicate error');
}
}

System.debug('Temp list>>' + temp);  // this keeps giving an empty list

 

Even though my list allCertNo has duplicate values, checkVal never turns false. 

What could I possibly be missing out on? 

 

Please help.

 

Much thanks.

Ruchika

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_EvolveSFDC_Evolve

Could u please post the List  allCertNo.... Everything seems fine in the code ..  We have to check for the list  also .   

 

May be some space.. . cooma .  or some thing Making difference in the String 

All Answers

Niket SFNiket SF

If you are going to check only duplicate values it's not matter how many time they are 

 

 Map<String,String> MapUnique = new Map<String,String>();

 

Set<String> checkDuplicates = new Set<String>();

 

for (String s : allCertNo ){

             if(!MapUnique.ContainsKey(s))

             { 

                MapUnique.put(s,s);

             }else

           checkDuplicates.add(s);

}

System.debug('============> These are the duplicate record ===>'+checkDuplicates);

SFDC_EvolveSFDC_Evolve

Could u please post the List  allCertNo.... Everything seems fine in the code ..  We have to check for the list  also .   

 

May be some space.. . cooma .  or some thing Making difference in the String 

This was selected as the best answer
RuchikaRuchika

@SFDC_Evolve -- Just caught the error. Realised that allCertNo was a Set. Changed it to a List and it worked. Thanks for the tip-off! 

 

Cheers,

Ruchika

RuchikaRuchika

@Nik -- My code worked with the List. So I'd rather noot use Maps. :)

Niket SFNiket SF

@Ruchika,

                  I love Map rather than list. It wil consume less time and less line of code. that  is the only reason. Other wise you are good. :)