Input: ArrayList of ArrayLists with email ids.
Output: Return ArrayList of email ids which appears in each inner ArrayList.
Ex: Input: [[[email protected], [email protected]],[[email protected],[email protected],[email protected]],[[email protected],[email protected]]]
Output: [[email protected]]
I have written the code using HashSet
and HashMap
and could get the required output but I'm not sure how optimized and efficient my method is.
public static List<String> Common(List<List<String>> email1){
List<String> res= new ArrayList<String>();
HashMap<String,Integer> map = new HashMap<String,Integer>();
Set<String> set ;
int cou = 0;
for( List list : email1)
{
set = new HashSet<String>();
List<String> temp = list;
for(String i: temp){
if(!set.contains(i)){
if(map.get(i) !=null){
map.put(i, map.get(i)+1);
}else{
map.put(i, 1);
}
}
}
cou++;
}
int size = email1.size();
for(String count: map.keySet()){
if(map.get(count)== size)
{
System.out.println(count);
res.add(count);
}
}
return res; // << this
}