“JS finden Duplikate in Array” Code-Antworten

Finden Sie Duplikate in Array JavaScript

let arr = [1, 2, 3, 4, 5, 5];
const seen = new Set();
const duplicates = arr.filter(n => seen.size === seen.add(n).size);
console.log(duplicates); // [ 5 ]
console.log(duplicates.length); // 1
slohobo

Holen Sie sich doppelte Werte von Array JavaScript

// This code was pasted by ProgrammerRimon, 2022-02-18
// Find dublicates numbers from array;

const a = [1,1,1,3,3,4,5,5,6,7,8,8,8,8,8,9,2];

function findDublicatesNumbersFromArray(a) {
    var d = [];
    for (let i = 0; i < a.length; i++) {
        var ct = a[i];
        var cmt = 0;
        for(var x = 0; x<a.length;++x) {
            if(ct === a[x]) {
                cmt++
                if(cmt > 1) {
                    d.push(a[i])
                }
            }
        }
    }
    return d.filter((i, ix)=> d.indexOf(i) === ix);
}

console.log(findDublicatesNumbersFromArray(a)) 
Homely Hamerkop

Überprüfen Sie in Array JavaScript nach Duplikaten

[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true

[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
Repulsive Rhinoceros

Doppelte Zahlen in einem Array JavaScript

[1, 1, 2, 2, 3].filter((element, index, array) => array.indexOf(element) !== index) // [1, 2]
Smarty Hearty

Suchen Sie doppelte Werte im Array JavaScript

const findDuplicates = (arr) => {
  let sorted_arr = arr.slice().sort(); // You can define the comparing function here. 
  // JS by default uses a crappy string compare.
  // (we use slice to clone the array so the
  // original array won't be modified)
  let results = [];
  for (let i = 0; i < sorted_arr.length - 1; i++) {
    if (sorted_arr[i + 1] == sorted_arr[i]) {
      results.push(sorted_arr[i]);
    }
  }
  return results;
}

let duplicatedArray = [9, 4, 111, 2, 3, 4, 9, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
Fancy Fish

JS finden Duplikate in Array

const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']

const count = names =>
  names.reduce((a, b) => ({ ...a,
    [b]: (a[b] || 0) + 1
  }), {}) // don't forget to initialize the accumulator

const duplicates = dict =>
  Object.keys(dict).filter((a) => dict[a] > 1)

console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
console.log(duplicates(count(names))) // [ 'Nancy' ]
Restu Wahyu Saputra

Ähnliche Antworten wie “JS finden Duplikate in Array”

Fragen ähnlich wie “JS finden Duplikate in Array”

Weitere verwandte Antworten zu “JS finden Duplikate in Array” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen