Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find duplicates 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
Comment

find duplicate values in 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)}`);
Comment

how to find duplicates in an array

const numbers = [1, 2, 3, 2, 4, 5, 5, 6];

const set = new Set(numbers);

const duplicates = numbers.filter(item => {
    if (set.has(item)) {
        set.delete(item);
    } else {
        return item;
    }
});

console.log(duplicates);
// [ 2, 5 ]
Comment

find duplicates in array

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

find duplicate values in 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)}`);
Comment

find duplicate element on 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 } 
Comment

Find / Display duplicate numbers in array

var input = [1, 2, 5, 5, 3, 1, 3, 1];
    var duplicates = input.reduce(function(acc, el, i, arr) {
    if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
    }, []);
    console.log(duplicates);
Comment

find-array-duplicates

npm i find-array-duplicates

import duplicates from 'find-array-duplicates'

const names = [
 { 'age': 36, 'name': 'Bob' },
 { 'age': 40, 'name': 'Harry' },
 { 'age': 1,  'name': 'Bob' }
]
 
duplicates(names, 'name').single()
// => { 'age': 36, 'name': 'Bob' }
Comment

find duplicates 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]
Comment

PREVIOUS NEXT
Code Example
Javascript :: react native gif dont work 
Javascript :: chart.js clear data 
Javascript :: how to change list item text color in react 
Javascript :: indexof method 
Javascript :: java hashmap get array of keys 
Javascript :: replace character inside a string in JavaScript 
Javascript :: node.js error handling process 
Javascript :: react native make safe view in mobile 
Javascript :: js check if all array values are the same 
Javascript :: Find duplicate or repeat elements in js array 
Javascript :: javascript foreach arrow function 
Javascript :: instalar bootstrap en react 
Javascript :: tableau js 
Javascript :: loop through nested json object typescript 
Javascript :: Get the current tab 
Javascript :: javascript continue with for Loop 
Javascript :: datatable set row id 
Javascript :: page reload detect in jquery 
Javascript :: axios node js 
Javascript :: buffer to image nodejs 
Javascript :: Appending the option element using jquery each function 
Javascript :: sum of all elements in array javascript 
Javascript :: javascript get cursor position without event 
Javascript :: get the length of an object vuejs 
Javascript :: create a simple website using javascript 
Javascript :: import error in react 
Javascript :: // Write a function that takes a number (a) as argument // Split a into its individual digits and return them in an array // Tipp: you might want to change the type of the number for the splitting 
Javascript :: trailing comma javascript 
Javascript :: apache react deploy "conf" 
Javascript :: module export in node js 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =