Javascript - Tìm các phần tử duy nhất từ hai mảng
Tìm các phần tử duy nhất từ hai mảng
Ví dụ:
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to convert the letters of a given string in alphabetical order.</title>
</head>
<script>
function difference(arr1,arr2) {
var a1= flatten(arr1,true);
var a2= flatten(arr2,true);
var a=[], diff=[];
for(var i=0;i< a1.length;i++)
a[a1[i]]=false;
for(i=0;i< a2.length;i++)
if(a[a2[i]]===true)
{ delete a[a2[i]];}
else a[a2[i]]=true;
for(var k in a)
diff.push(k);
return diff;
}
var flatten = function(a, shallow,r){
if(!r){ r = [];}
if (shallow) {
return r.concat.apply(r,a);
}
for(i=0; i< a.length; i++){
if(a[i].constructor == Array){
flatten(a[i],shallow,r);
}else{
r.push(a[i]);
}
}
return r;
};
document.write(difference([1, 2, 3], [100, 2, 1, 10])+"<br/>");
document.write(difference([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]])+"<br/>");
document.write(difference([1, 2, 3], [100, 2, 1, 10])+"<br/>");
</script>
<body>
</body>
</html>
Lưu đồ thuật toán: