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
chaitanyakuamar Teruchaitanyakuamar Teru 

apex develoer

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'};

print the common values
            
Foram Rana RForam Rana R
Hi ChaitanyaKumar,

Please try the below code
 
List<String> firstcolourlist = new list<String>{'red','blue','yellow','orange','blue','red'};
List<String> secondcolourlist = new list<String>{'blue','black','yellow','green','blue'};
Set<String> setOdcolour = new Set<String>();

setOdcolour.addAll(firstcolourlist);
setOdcolour.addAll(secondcolourlist);

System.debug('@@@ Result = '+setOdcolour);
Mark, it as the best answers if you resolve the issue.


Thanks,
Foram Rana
 
chaitanyakuamar Teruchaitanyakuamar Teru
thanks for responding 
bt my question is to compare both list's & print the common values along with their count 
like 
output be like ={blue=4,yellow=2,red=2} 
sachinarorasfsachinarorasf
Hi Chaitanya,

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