“Periksa duplikat di array javascript” 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

JavaScript Periksa apakah array memiliki duplikat

function hasDuplicates(array) {
    return (new Set(array)).size !== array.length;
}
Attractive Addax

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

Jawaban yang mirip dengan “Periksa duplikat di array javascript”

Pertanyaan yang mirip dengan “Periksa duplikat di array javascript”

Lebih banyak jawaban terkait untuk “Periksa duplikat di array javascript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya