This was an Amazon interview question I had an my answer was
function intersection ( A , B )
{
var C = [];
for ( var a in A ) if ( B.indexOf(a) != -1 ) C.push(a);
return C;
}
and he asked what the order of complexity was and I said, and I quote exactly,
O(m * n) where m=A.length and n=B.length
and he was saying there's a better way to do it and I was like WTF??????? He was saying use A and B as Objects and I was like
"But you said these were arrays That was your question!!!!"
Can someone help me out here?
Bbefore you start, then you can do it in O(n).for ... into loop through arrays.AandBare arrays, this won't do what it pretends to. Infor (var a in A) { ... }the variableawill hold the indices.