I just learn Big O notation, and I went through a few mysterious puzzles, and I just thought I would check with people if I was thinking about things right.
In Javascript, will this be considered an O (n) time solution for finding common elements across two arrays? Or does the language search in the object and iterate through n elements inside the object in the same way that it iterates through an array?
function findCommon (input, input2){
var key = {};
var out = [];
for(var i=0; i<input.length; i++){
key[input[i]] = true;
}
for(var j=0; j<input2.length; j++){
if(key[input2[j]] == true){
out.push(input2[j]);
}
}
return out;
}
findCommon ([1, 2, 4, 6, 7], [3, 4, 5, 7]) -> [4, 7]
source
share