You need to sign in to do that
Don't have an account?
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
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
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);
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
@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
@Nik -- My code worked with the List. So I'd rather noot use Maps. :)
@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. :)