“Temukan duplikat di array” Kode Jawaban

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

Temukan duplikat di array

function findDuplicates(arr) {
	const duplicates = new Set()
  
  return arr.filter(item => {
  	if (duplicates.has(item)) {
    	return true
    }
    duplicates.add(item)
    return false
  })
}
Raserad

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, 9, 4, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
Fancy Fish

Temukan elemen duplikat pada array

const order = ["apple", "banana", "orange", "banana", "apple", "banana"];

const result = order.reduce(function (prevVal, item) {
    if (!prevVal[item]) {
        // if an object doesn't have a key yet, it means it wasn't repeated before
        prevVal[item] = 1;
    } else {
        // increase the number of repetitions by 1
        prevVal[item] += 1;
    }

    // and return the changed object
    return prevVal;
}, {}); // The initial value is an empty object.

console.log(result); // { apple: 2, banana: 3, orange: 1 } 
Cruel Cormorant

Temukan Duplikat Array JavaScript

let arr = [1, 7, 8, 9, 10, 20, 33, 0, 20, 7, 1]
console.log([... new Set(arr)]
// Output : [1, 7, 8, 9, 10, 20, 33, 0]
Wide-eyed Wolf

Jawaban yang mirip dengan “Temukan duplikat di array”

Pertanyaan yang mirip dengan “Temukan duplikat di array”

Lebih banyak jawaban terkait untuk “Temukan duplikat di array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya