You need to sign in to do that
Don't have an account?
chaitanyakuamar Teru
apex code logic
1=list<String> firstcolourlist=new list<String>{'red','blue','yellow','orange','blue','red'};
2= list<String>secondcolourlist=new list<String>{'blue','black','yellow','green','blue'};
if we compare list1 - list2 or list2 - list1 we have to print common values in both lists along with their count
2= list<String>secondcolourlist=new list<String>{'blue','black','yellow','green','blue'};
if we compare list1 - list2 or list2 - list1 we have to print common values in both lists along with their count
Please execute the following lines of code to get the required output:
List<String> Flist = new list<String>{'red','blue','yellow','orange','blue','red'};
List<String> Slist = new list<String>{'blue','black','yellow','green','blue'};
List<String> CommonValueList = new list<String>();
for(String var : Flist){
if(Slist.contains(var) && !CommonValueList.contains(var)){
CommonValueList.add(var);
}
}
List<String> MergedList = new list<String>();
MergedList.addAll(Flist);
MergedList.addAll(Slist);
Map<String, List<String>> requiredMap = new Map<String, List<String>>();
for(String obj : MergedList){
if(requiredMap.containsKey(obj)){
requiredMap.get(obj).add(obj);
}else{
requiredMap.put(obj,new list<String>());
requiredMap.get(obj).add(obj);
}
}
// Commonvaluelist contains the common values and iterate it over map to get the count values
for(String x: CommonValueList){
System.debug(x +' : '+requiredMap.get(x).size());
}
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com