I'm coding a javascript snippet that searches for a match between two arrays. I know how to search it brutally (array takes a number input, searches each value of the other arrays to see if they match do this over and over again) but it is very inefficient. If anyone knows a way to search for a common value between two arrays so that it takes minimum time possible, please help me.
var 1 = ["bob", "Sophie"];
var 2 = ["Sherry", "Gerard", "Joseph"];
for(var i; i <= 1.length; i++){
switch(1[i]){
case 1[i] === 2[1]:
console.log("They match!");
break;
case 1[i] === 2[2]:
console.log("They match!");
break;
case 1[i] === 2[3]:
console.log("They match!");
break;
case default:
console.log("No matches found.");
}
}
}
PS Don mind the syntax errors, this is a "Rough draft" of the code. I put it just so that you could see what I meant.