Match the elements from two arrays using HashMap

Run Settings
LanguageJava
Language Version
Run Command
class Main { public static boolean matchTheElements(String [] array1, String [] array2){ for (int i = 0; i<array1.length; i++){ for (int j = 0; j<array2.length; j++){ if (array1[i] == array2[j]){ return true; } } } return false; } public static void main(String[] args) { String [] array1 = {"a", "b", "c", "d"}; String [] array2 = {"a","v", "t"}; boolean abc = matchTheElements(array1, array2); System.out.println ("Matching element is found! " + abc); } }
class MatchingElements { public static boolean matchTheElements(HashMap<String, Boolean> map, String [] array2){ for (int j = 0; j<array2.length; j++){ System.out.println("Value from HashMap : " + map.get(array2[j])) if (map.get(array2[j]) == true){ return true; } } } return false; } public static void main(String[] args) { String [] array1 = {"a", "b", "c", "d"}; String [] array2 = {"a","v", "t"}; //Create a HashMap and strore the elements within. HashMap<String, Boolean> map = new HashMap<String, Boolean>(); for (int i = 0; i<array1.length; i++){ map.put(array[i], true); } boolean abc = matchTheElements(map, array2); System.out.println ("Matching element is found! " + abc); } }
Editor Settings
Theme
Key bindings
Full width
Lines