這是一個(gè)經(jīng)典的算法題拘荡,首先有兩個(gè)有序數(shù)組a = [1, 2, 3, 6, 7, 9]
勿锅,b = [2, 4, 6, 8, 10]
荧缘,然后實(shí)現(xiàn)它倆的合并皆警,并去掉重復(fù)值,即我的數(shù)組c
為[1, 2, 3, 4, 6, 7, 8, 9, 10]
代碼如下:
function merge(a,b)
{
var i = 0,j = 0;
var c = [];
while(i < a.length && j < b.length){
if (a [i] < b [j]){
c.push(a[i++]);
}
else if(a [i] == b [j]){
c.push(a[i++]);
j++;
}
else {
c.push(b[j++]);
}
}
while(i < a.length){
c.push(a[i++]);
}
while(j < b.length){
c.push(b[j++]);
}
return c;
}