//Array.indexOf was introduced in javascript 1.6 (ECMA-262)//We need to implement it explicitly for other browsers,if (!Array.prototype.indexOf){Array.prototype.indexOf = function(elt, from){var len = this.length >>> 0;
for (; from < len; from++){if (from in this &&this[from] === elt)return from;}return -1;};}//now, on to the problem
var array1 = ["Vijendra","Singh"];var array2 = ["Singh", "Shakya"];
var merged = array1.concat(array2);var t;for(i = 0; i < merged.length; i++)if((t = merged.indexOf(i + 1, merged[i])) != -1){merged.splice(t, 1);i--;//in case of multiple occurrences}
Array.prototype.add = function(b){var a = this.concat(); // clone current objectif(!b.push || !b.length) return a; // if b is not an array, or empty, then return a unchangedif(!a.length) return b.concat(); // if original is empty, return b
// go through all the elements of bfor(var i = 0; i < b.length; i++){// if b's value is not in a, then add itif(a.indexOf(b[i]) == -1) a.push(b[i]);}return a;}
// Example:console.log([1,2,3].add([3, 4, 5])); // will output [1, 2, 3, 4, 5]
/*** Returns with the union of the given arrays.** @param Any amount of arrays to be united.* @returns {array} The union array.*/function uniteArrays(){var union = [];for (var argumentIndex = 0; argumentIndex < arguments.length; argumentIndex++){eachArgument = arguments[argumentIndex];if (typeof eachArgument !== 'array'){eachArray = eachArgument;for (var index = 0; index < eachArray.length; index++){eachValue = eachArray[index];if (arrayHasValue(union, eachValue) == false)union.push(eachValue);}}}
return union;}
function arrayHasValue(array, value){ return array.indexOf(value) != -1; }
var a = [1, 2, 3], b = [101, 2, 1, 10]var c = a.concat(b)var d = c.filter((item, pos) => c.indexOf(item) === pos)
console.log(d) // d is [1, 2, 3, 101, 10]
编辑
正如建议的那样,更具性能智慧的解决方案是在与a连接之前过滤掉b中的唯一项:
var a = [1, 2, 3], b = [101, 2, 1, 10]var c = a.concat(b.filter((item) => a.indexOf(item) < 0))
console.log(c) // c is [1, 2, 3, 101, 10]
function merge(a, b) {var hash = {};var i;
for (i = 0; i < a.length; i++) {hash[a[i]] = true;}for (i = 0; i < b.length; i++) {hash[b[i]] = true;}return Object.keys(hash);}
var array1 = ["Vijendra", "Singh"];var array2 = ["Singh", "Shakya"];
var array3 = merge(array1, array2);
console.log(array3);
function es3Merge(a, b) {var hash = {},i = (a = a.slice(0)).length,e;
while (i--) {hash[a[i]] = 1;}
for (i = 0; i < b.length; i++) {hash[e = b[i]] || a.push(e);}
return a;};
function indiceMerge(a1, a2) {var ai = [];for (var x = 0; x < a2.length; x++) {ai.push(x)};
for (var x = 0; x < a1.length; x++) {for (var y = 0; y < ai.length; y++) {if (a1[x] === a2[ai[y]]) {ai.splice(y, 1);y--;}}}
for (var x = 0; x < ai.length; x++) {a1.push(a2[ai[x]]);}
return a1;}
function findMerge(a1, a2) {var len1 = a1.length;
for (var x = 0; x < a2.length; x++) {var found = false;
for (var y = 0; y < len1; y++) {if (a2[x] === a1[y]) {found = true;break;}}
if(!found){a1.push(a2.splice(x--, 1)[0]);}}
return a1;}
function arrayMerge(base, addendum){var out = [].concat(base);for(var i=0,len=addendum.length;i<len;i++){if(base.indexOf(addendum[i])<0){out.push(addendum[i]);}}return out;}
var MergeArrays=function(arrayOne, arrayTwo, equalityField) {var mergeDictionary = {};
for (var i = 0; i < arrayOne.length; i++) {mergeDictionary[arrayOne[i][equalityField]] = arrayOne[i];}
for (var i = 0; i < arrayTwo.length; i++) {mergeDictionary[arrayTwo[i][equalityField]] = arrayTwo[i];}
return $.map(mergeDictionary, function (value, key) { return value });}
Array.prototype.union = function (other_array) {/* you can include a test to check whether other_array really is an array */other_array.forEach(function(v) { if(this.indexOf(v) === -1) {this.push(v);}}, this);}
/*** De-duplicate an array keeping only unique values.* Use hash table (js object) to filter-out duplicates.* The order of array elements is maintained.* This algorithm is particularly efficient for large arrays (linear time).*/function arrayUniqueFast(arr) {var seen = {};var result = [];var i, len = arr.length;for (i = 0; i < len; i++) {var item = arr[i];// hash table lookupif (!seen[item]) {result.push(item);seen[item] = true;}}return result;}
///// testvar array1 = ["Vijendra", "Singh"];var array2 = ["Singh", "Shakya"];
var result = arrayUniqueFast(array1.concat(array2));document.write('<br>result: ' + result);
/*** This function merging only arrays unique values. It does not merges arrays in to array with duplicate values at any stage.** @params ...args Function accept multiple array input (merges them to single array with no duplicates)* it also can be used to filter duplicates in single array*/function arrayDeDuplicate(...args){let set = new Set(); // init Set object (available as of ES6)for(let arr of args){ // for of loops through valuesarr.map((value) => { // map adds each value to Set objectset.add(value); // set.add method adds only unique values});}return [...set]; // destructuring set object back to array object// alternativly we culd use: return Array.from(set);}
someSource().reduce(...).filter(...).map(...)// and now you want to concat array2 and deduplicate:.concat(array2).filter((value, pos, arr)=>arr.indexOf(value)===pos)// and keep chaining stuff.map(...).find(...)// etc
/*** Merges two or more arrays keeping unique items. This method does* not change the existing arrays, but instead returns a new array.*/function union<T>(...arrays: T[]) {return [...new Set([...arrays].flat())];}
var array1 = ["Vijendra","Singh","Singh"];var array2 = ["Singh", "Shakya", "Shakya"];const reducer = (accumulator, currentValue) => accumulator.includes(currentValue) ? accumulator : [...accumulator, currentValue];
console.log(array2.reduce(reducer, array1.reduce(reducer, [])));
// a reduce on first array is needed to ensure a deduplicated array used as initial value on the second array being reduced
//1.merge two array into one array
var arr1 = [0, 1, 2, 4];var arr2 = [4, 5, 6];
//for merge array we use "Array.concat"
let combineArray = arr1.concat(arr2); //output
alert(combineArray); //now out put is 0,1,2,4,4,5,6 but 4 reapeat
//2.same thing with "Spread Syntex"
let spreadArray = [...arr1, ...arr2];
alert(spreadArray); //now out put is 0,1,2,4,4,5,6 but 4 reapete
/*if we need remove duplicate element method use are1.Using set2.using .filter3.using .reduce*/
// https://stackoverflow.com/a/10499519/860099function A(arr1,arr2) {return _.union(arr1,arr2)}
// https://stackoverflow.com/a/53149853/860099function B(arr1,arr2) {return _.unionWith(arr1, arr2, _.isEqual);}
// https://stackoverflow.com/a/27664971/860099function C(arr1,arr2) {return [...new Set([...arr1,...arr2])]}
// https://stackoverflow.com/a/48130841/860099function D(arr1,arr2) {return Array.from(new Set(arr1.concat(arr2)))}
// https://stackoverflow.com/a/23080662/860099function E(arr1,arr2) {return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))}
// https://stackoverflow.com/a/28631880/860099function G(arr1,arr2) {var hash = {};var i;
for (i = 0; i < arr1.length; i++) {hash[arr1[i]] = true;}for (i = 0; i < arr2.length; i++) {hash[arr2[i]] = true;}return Object.keys(hash);}
// https://stackoverflow.com/a/13847481/860099function H(a, b){var hash = {};var ret = [];
for(var i=0; i < a.length; i++){var e = a[i];if (!hash[e]){hash[e] = true;ret.push(e);}}
for(var i=0; i < b.length; i++){var e = b[i];if (!hash[e]){hash[e] = true;ret.push(e);}}
return ret;}
// https://stackoverflow.com/a/1584377/860099function J(arr1,arr2) {function arrayUnique(array) {var a = array.concat();for(var i=0; i<a.length; ++i) {for(var j=i+1; j<a.length; ++j) {if(a[i] === a[j])a.splice(j--, 1);}}
return a;}
return arrayUnique(arr1.concat(arr2));}
// https://stackoverflow.com/a/25120770/860099function L(array1, array2) {const array3 = array1.slice(0);let len1 = array1.length;let len2 = array2.length;const assoc = {};
while (len1--) {assoc[array1[len1]] = null;}
while (len2--) {let itm = array2[len2];
if (assoc[itm] === undefined) { // Eliminate the indexOf callarray3.push(itm);assoc[itm] = null;}}
return array3;}
// https://stackoverflow.com/a/39336712/860099function M(arr1,arr2) {const comp = f => g => x => f(g(x));const apply = f => a => f(a);const flip = f => b => a => f(a) (b);const concat = xs => y => xs.concat(y);const afrom = apply(Array.from);const createSet = xs => new Set(xs);const filter = f => xs => xs.filter(apply(f));
const dedupe = comp(afrom) (createSet);
const union = xs => ys => {const zs = createSet(xs);return concat(xs) (filter(x => zs.has(x)? false: zs.add(x)) (ys));}
return union(dedupe(arr1)) (arr2)}
// -------------// TEST// -------------
var array1 = ["Vijendra","Singh"];var array2 = ["Singh", "Shakya"];
[A,B,C,D,E,G,H,J,L,M].forEach(f=> {console.log(`${f.name} [${f([...array1],[...array2])}]`);})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
This snippet only presents functions used in performance tests - it not perform tests itself!