55 Contains Common Item 2 Arrays

Run Settings
LanguageJavaScript
Language Version
Run Command
const array1 = ['a', 'b', 'c', 'x']; const array2 = ['z', 'y', 'a']; function containsCommonItem2(arr1, arr2) { // loop through 1st array and screate an object where properties === items in the array let map = {}; for(let i=0; i<arr1.length; i++) { if(!map[arr1[i]]) { // let us make a map with items const item = arr1[i]; // so that map[item] = true; } } // loop through 2nd array and check if item in 2nd array exists on created object for(let j=0; j<arr2.length; j++) { if(map[arr2[j]]) { return true; } } return false; } // O(a+b) Time Complexity // O(a) Space Complexity containsCommonItem2(array1, array2) function containsCommonItem3(arr1, arr2) { return arr1.some(item => arr2.includes(item)); } containsCommonItem3(array1, array2)
Editor Settings
Theme
Key bindings
Full width
Lines