面试题:有两个有序数组,判断是否有元素同时存在于两个数组中,最优解
作者:互联网
有两个int型数组,已排序,判断是否有元素同时存在于a和b中
var arr1 = [1,2,4,5]
var arr2 = [3,4,6,7,8]
function checkOwnSameItem(arr1,arr2) {
var len1 = arr1.length;
var len2 = arr2.length;
var indexA = indexB = 0;
while (indexA < len1 && indexB < len2) {
var itemA = arr1[indexA];
var itemB = arr2[indexB];
if (itemA == itemB )
{
console.log("====find the first same item:"+itemA+",....in arr1 index:"+indexA+",...arr2 index:"+indexB);
return true ;
}
else if(itemA < itemB) {
indexA ++;
}
else
{
indexB ++;
}
}
}
checkOwnSameItem(arr1,arr2);
标签:面试题,数组,indexB,indexA,arr2,arr1,var,最优,itemA 来源: https://blog.csdn.net/karaa/article/details/90214607