containsCommonItem

Run Settings
LanguageJavaScript
Language Version
Run Command
const array1 = ['a', 'b', 'c', 'x'] const array2 = ['z', 'y', 'a'] // if we had two empty arrays [] this would still work as in JS object properties // are turned into strings when created // O(a + b) time complexity // O(a) space complexity function containsCommonItem(arr1, arr2) { // loop through first array and create object where properties === items in // the array // can we assume always 2 params? let map = {} for (let i = 0; i < arr1.length; i++) { if (!map[i]) { const item = arr1[i] map[item] = true } } // loop through second array and check if item in second array exists on // created object for (let i = 0; i < arr2.length; i++) { let checkVar = map[arr2[i]] if (map[arr2[i]]) { return true } } return false } let resultVar = containsCommonItem(array1, array2) console.log(resultVar) // Language specific to JS function containsCommonItem2(arr1, arr2) { // iterate arr1 and if arr2 includes an element presen in arr1 return true return arr1.some(item => arr2.include(item)); }
Editor Settings
Theme
Key bindings
Full width
Lines