7
\$\begingroup\$

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 
    }
\$\endgroup\$
0

3 Answers 3

4
\$\begingroup\$

Using a HashSet is fine, but you don't need to keep the count of occurrence of each email, you can just keep in the HashSet those that appear in all list, and remove all other emails.

//Need to check if email1 is empty before this step!
HashSet<String> set = new HashSet(); 
set.addAll(email1.get(0));
for(int i = 1; i < email1.size(); i++){
    HashSet<String> tmp = new HashSet();
    for(String email : email1.get(i)){
        if(set.contains(email)){//If set contains this email, keep it!
           tmp.add(email);
        }
    }
    set = tmp;//Important step
}
//All emails will be in the set
System.out.println(set);
\$\endgroup\$
2
\$\begingroup\$

Here is my answer using retainAll from the Set interface:

public static List<String> commonEmails(List<List<String>> emailLists) {
    if (emailLists.size() > 0) {
        Set<String> commonEmails = new HashSet<String>(emailLists.get(0));
        for (int i = 1; i < emailLists.size(); i++) {
            commonEmails.retainAll(emailLists.get(i));
        }
        return new ArrayList<String>(commonEmails);
    } else {
        return new ArrayList<String>();
    }
}
\$\endgroup\$
0
2
\$\begingroup\$

Collections.retainAll() can act as an intersection operator.

Note that your method can be more liberal in the types that it accepts, if you use generics effectively.

public static <T> List<T> commonList(Collection<? extends List<T>> collectionOfLists) {
    if (collectionOfLists.isEmpty()) {
        return Collections.emptyList();
    }
    Iterator<? extends List<T>> iter = collectionOfLists.iterator();
    List<T> first = new ArrayList<T>(iter.next());
    Set<T> common = new HashSet<T>(first);
    while (iter.hasNext()) {
        common.retainAll(iter.next());
    }
    first.retainAll(common);
    return first;
}

Assuming that the order of the resulting entries doesn't matter (I don't see why it should), you should return a Set instead of a List.

public static <T> Set<T> commonSet(Collection<? extends Collection<T>> collectionOfCollections) {
    if (collectionOfCollections.isEmpty()) {
        return Collections.emptySet();
    }
    Iterator<? extends Collection<T>> iter = collectionOfCollections.iterator();
    Set<T> common = new HashSet<T>(iter.next());
    while (iter.hasNext()) {
        common.retainAll(iter.next());
    }
    return common;
}

By the Single Responsibility Principle, I don't recommend polluting this function that computes the intersection with System.out.println() calls. Keep the printing in a separate method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.