import java.util.*;

public class Babel
{
    public Babel (boolean debug)
    {
        _debug = debug;
    }

    public void translate (Vector<Food> foods)
    {
        Vector<String> mapped = new Vector<String>();

        for (int i = 0; i < foods.size(); i++)
        {
            Food toCheck = foods.elementAt(i);
            Vector<String> ingredients = toCheck.getIngredients();

            for (int j = 0; j < ingredients.size(); j++)
            {
                String ingredient = ingredients.elementAt(j);

                for (int k = 0; k < foods.size(); k++)
                {
                    Food compare = foods.elementAt(k);

                    if (!toCheck.equals(compare))
                    {
                        if (compare.getIngredients().contains(ingredient))
                        {
                            if (_debug)
                                System.out.println("Ingredient "+ingredient+" found in\n"+compare);

                            // now check where this is also used

                            if (mapped.contains(ingredient))
                                System.out.println("Already added "+ingredient);
                            else
                                mapped.add(ingredient);
                        }
                    }
                }
            }
        }

        System.out.println("Mapped "+mapped.size());

        for (int i = 0; i < mapped.size(); i++)
        {
            System.out.println("Mapped "+i+": "+mapped.elementAt(i));
        }
    }
    
    private boolean _debug;
}
@@ -12,16 +12,40 @@
 .util.*;
+%0Aimport java.util.Map.*;
 %0A%0Apublic
@@ -144,12 +144,11 @@
 lic 
-void
+int
  tra
@@ -184,24 +184,40 @@
    %7B%0A       
+ HashMap%3CString,
  Vector%3CStri
@@ -219,16 +219,17 @@
 %3CString%3E
+%3E
  mapped 
@@ -233,16 +233,32 @@
 ed = new
+ HashMap%3CString,
  Vector%3C
@@ -264,20 +264,102 @@
 %3CString%3E
+%3E
 ();%0A
+        HashMap%3CString, Integer%3E count = new HashMap%3CString, Integer%3E();%0A        
 %0A       
@@ -471,376 +471,307 @@
     
-Vector%3CString%3E ingredients = toCheck.getIngredients();%0A%0A            for (int j = 0; j %3C ingredients.size(); j++)%0A            %7B%0A                String ingredient = ingredients.elementAt(j);%0A%0A                for (int k = 0; k %3C foods.size(); k++)%0A                %7B%0A                    Food compare = foods.elementAt(k);%0A%0A                    if (!toCheck.equals(compare
+%0A            toCheck.getIngredients().forEach(ingredient -%3E count.put(ingredient, count.getOrDefault(ingredient, 0) + 1));%0A%0A            for (String allergen : toCheck.getAllergens())%0A            %7B%0A                mapped.computeIfAbsent(allergen, key -%3E new Vector%3CString%3E(toCheck.getIngredients()
 ))
+;
 %0A   
@@ -787,77 +787,51 @@
     
-    %7B%0A                        if (compare.getIngredients().contains(i
+mapped.get(allergen).retainAll(toCheck.getI
 ngre
@@ -827,34 +827,37 @@
 ck.getIngredient
+s(
 ))
+;
 %0A               
@@ -857,403 +857,337 @@
     
-            %7B%0A                            if (_debug)%0A                                System.out.println(%22Ingredient %22+ingredient+%22 found in%5Cn%22+compare);%0A%0A                            // now check where this is also used%0A%0A                            if (mapped.contains(ingredient))%0A                                System.out.println(%22Already added %22+ingredient);%0A                            else
+%7D%0A        %7D%0A%0A        if (_debug)%0A            System.out.println(%22Mapped %22+mapped.size());%0A%0A        int totalOccurrences = count.entrySet().stream().filter(%0A                                ingredient -%3E mapped.entrySet().stream()%0A                                .noneMatch(entry -%3E entry.getValue().contains(ingredient.getKey())))
 %0A   
@@ -1219,31 +1219,34 @@
     
+.
 map
-ped.add(ingredient);
+ToInt(Entry::getValue)
 %0A   
@@ -1270,266 +1270,65 @@
     
-%7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A%0A        System.out.println(%22Mapped %22+mapped.size());%0A%0A        for (int i = 0; i %3C mapped.size(); i++)%0A        %7B%0A            System.out.println(%22Mapped %22+i+%22: %22+mapped.elementAt(i));%0A        %7D
+        .sum();%0A        %0A        return totalOccurrences;
 %0A   

Update Babel.java