programming-examples/js/Array/Write a JavaScript function to merge two arrays and removes all duplicates elements..js

25 lines
520 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function merge_array(array1, array2) {
var result_array = [];
var arr = array1.concat(array2);
var len = arr.length;
var assoc = {};
while(len--) {
var item = arr[len];
if(!assoc[item])
{
result_array.unshift(item);
assoc[item] = true;
}
}
return result_array;
}
var array1 = [1, 2, 3];
var array2 = [2, 30, 1];
console.log(merge_array(array1, array2));