“JS menemukan duplikat di array” Kode Jawaban

Temukan duplikat di 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

Dapatkan nilai duplikat dari 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

Periksa duplikat di array javascript

[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

duplikat nomor dalam array javascript

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

Temukan nilai duplikat di 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 menemukan duplikat di 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

Jawaban yang mirip dengan “JS menemukan duplikat di array”

Pertanyaan yang mirip dengan “JS menemukan duplikat di array”

Lebih banyak jawaban terkait untuk “JS menemukan duplikat di array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya