This is an interview question one of my colleague asked, so thought of sharing.
Below is code snippet
List<String> stringList = new List<String>{'One','two','Three','Four','One','two'};
set<String> setString = new set<String>();
set<String> duplicatesetString = new set<String>();
for(String s:stringList ){
if(!setString.add(s)){
duplicatesetString .add(s);
}
}
System.debug('duplicatelistString----'+duplicatesetString );
System.debug('duplicatelistString----'+duplicatesetString.add('ten'));
Below is code snippet
List<String> stringList = new List<String>{'One','two','Three','Four','One','two'};
set<String> setString = new set<String>();
set<String> duplicatesetString = new set<String>();
for(String s:stringList ){
if(!setString.add(s)){
duplicatesetString .add(s);
}
}
System.debug('duplicatelistString----'+duplicatesetString );
System.debug('duplicatelistString----'+duplicatesetString.add('ten'));
No comments:
Post a Comment