算法要求:
Your goal in this kata is to implement an difference function, which subtracts one list from another.
It should remove all values from list a, which are present in list b.
c方法,雙重循環:
function array_diff(a, b) {
var bLength = b.length;
for(var i = 0; i<bLength; i++){
for(var j = 0; j<a.length; ){
if(b[i]==a[j]){
a.splice(j,1);
}else {
j++
}
}
}
return a;
}
js方法 ,調用自帶的一個indexOf:
function array_diff(a, b) {
for(var i = 0;i<a.length;){
if(b.indexOf(a[i])>-1){
a.splice(i,1);
}else{
i++;
}
}
return a;
}
不知道C是怎麼刪除數組中某一元素的????