1988 Stimmen

Zusammenführen von zwei Arrays in JavaScript und Duplizieren von Elementen

Ich habe zwei JavaScript-Arrays:

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

Ich möchte, dass die Ausgabe so aussieht:

var array3 = ["Vijendra","Singh","Shakya"];

Im Ausgabe-Array sollten wiederholte Wörter entfernt werden.

Wie kann ich zwei Arrays in JavaScript zusammenführen, so dass ich nur die eindeutigen Elemente aus jedem Array in der gleichen Reihenfolge erhalte, in der sie in die ursprünglichen Arrays eingefügt wurden?

19voto

Kamil Kiełczewski Punkte 69048

Leistung

Heute 2020.10.15 führe ich Tests auf MacOs HighSierra 10.13.6 mit Chrome v86, Safari v13.1.2 und Firefox v81 für ausgewählte Lösungen durch.

Ergebnisse

Für alle Browser

  • Lösung H ist schnell/schnellste
  • Lösungen L ist schnell
  • Lösung D ist auf Chrome bei großen Arrays am schnellsten
  • Lösung G ist schnell auf kleinen Arrays
  • Lösung M ist am langsamsten für kleine Arrays
  • Lösungen E sind am langsamsten für große Arrays

enter image description here

Einzelheiten

Ich führe 2 Testfälle durch:

  • für 2-Elemente-Arrays - Sie können es ausführen HIER
  • für 10000-Elemente-Arrays - Sie können es ausführen HIER

zu Lösungen A , B , C , D , E , G , H , J , L , M im folgenden Ausschnitt dargestellt

// https://stackoverflow.com/a/10499519/860099
function A(arr1,arr2) {
  return _.union(arr1,arr2)
}

// https://stackoverflow.com/a/53149853/860099
function B(arr1,arr2) {
  return _.unionWith(arr1, arr2, _.isEqual);
}

// https://stackoverflow.com/a/27664971/860099
function C(arr1,arr2) {
  return [...new Set([...arr1,...arr2])]
}

// https://stackoverflow.com/a/48130841/860099
function D(arr1,arr2) {
  return Array.from(new Set(arr1.concat(arr2)))
}

// https://stackoverflow.com/a/23080662/860099
function E(arr1,arr2) {
  return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))
}

// https://stackoverflow.com/a/28631880/860099
function 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/860099
function 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/860099
function 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/860099
function 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 call
            array3.push(itm);
            assoc[itm] = null;
        }
    }

    return array3;
}

// https://stackoverflow.com/a/39336712/860099
function 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!

Und hier ein Beispiel für einen Testlauf für Chrome

enter image description here

UPDATE

Ich habe die Fälle F, I und K entfernt, weil sie die Eingabefelder verändern und der Benchmark falsche Ergebnisse liefert.

17voto

Nick Retallack Punkte 18274

Warum benutzen Sie nicht ein Objekt? Es sieht so aus, als würden Sie versuchen, eine Menge zu modellieren. Dabei wird die Reihenfolge jedoch nicht beibehalten.

var set1 = {"Vijendra":true, "Singh":true}
var set2 = {"Singh":true,  "Shakya":true}

// Merge second object into first
function merge(set1, set2){
  for (var key in set2){
    if (set2.hasOwnProperty(key))
      set1[key] = set2[key]
  }
  return set1
}

merge(set1, set2)

// Create set from array
function setify(array){
  var result = {}
  for (var item in array){
    if (array.hasOwnProperty(item))
      result[array[item]] = true
  }
  return result
}

14voto

user1079877 Punkte 8419

Für ES6 nur eine Zeile:

a = [1, 2, 3, 4]
b = [4, 5]
[...new Set(a.concat(b))]  // [1, 2, 3, 4, 5]

12voto

Zigri2612 Punkte 2109

Die beste Lösung...

Sie können dies direkt in der Browserkonsole überprüfen, indem Sie auf...

Ohne Duplikat

a = [1, 2, 3];
b = [3, 2, 1, "prince"];

a.concat(b.filter(function(el) {
    return a.indexOf(el) === -1;
}));

Mit Duplikat

["prince", "asish", 5].concat(["ravi", 4])

Wenn Sie ohne Duplikate wollen, können Sie eine bessere Lösung von hier versuchen - Shouting Code .

[1, 2, 3].concat([3, 2, 1, "prince"].filter(function(el) {
    return [1, 2, 3].indexOf(el) === -1;
}));

Versuchen Sie es mit der Konsole des Browsers Chrome

 f12 > console

Ausgabe:

["prince", "asish", 5, "ravi", 4]

[1, 2, 3, "prince"]

10voto

Hero Qu Punkte 881

Mein anderthalb Penny:

Array.prototype.concat_n_dedupe = function(other_array) {
  return this
    .concat(other_array) // add second
    .reduce(function(uniques, item) { // dedupe all
      if (uniques.indexOf(item) == -1) {
        uniques.push(item);
      }
      return uniques;
    }, []);
};

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

var result = array1.concat_n_dedupe(array2);

console.log(result);

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X