How to find common elements in multiple lists?

Viewed 2085

I have a list of list (nest list). I need to find the common elements between those.

Example would be 
[1,3,5],
[1,6,7,9,3],
[1,3,10,11]

should result in [1,3]

If not using the retainAll method of HashSet, how to iterate all the element to find?

Thanks,

3 Answers

This code is written in Apex(similar to java)

List<Integer> a = new List<Integer>{1,4,7,8};
List<Integer> b = new List<Integer>{2,5,3,6,4,8,9};
List<Integer> c = new List<Integer>{9,5,2,7,4};


for(integer i=0;i<a.size();i++){
    if(b.contains(a[i]) && c.contains(a[i])) {
        system.debug(a[i]);
    }
}

Output will be 4

Related