Answers for "find duplicate values in array javascript"

2

how to get duplicate values from array in javascript

var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];

console.log([...new Set(
  array.filter((value, index, self) => self.indexOf(value) !== index))]
);
Posted by: Guest on June-19-2020
1

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
  })
}
Posted by: Guest on August-06-2021
2

javascript find duplicate in array

// JavaScript - finds if there is duplicate in an array. 
// Returns True or False.

const isThereADuplicate = function(arrayOfNumbers) {
    // Create an empty associative array or hash. 
    // This is preferred,
    let counts = {};
    // // but this also works. Comment in below and comment out above if you want to try.
    // let counts = [];

    for(var i = 0; i <= arrayOfNumbers.length; i++) {
        // As the arrayOfNumbers is being iterated through,
        // the counts hash is being populated.
        // Each value in the array becomes a key in the hash. 
        // The value assignment of 1, is there to complete the hash structure.
        // Once the key exists, meaning there is a duplicate, return true.
        // If there are no duplicates, the if block completes and returns false.
        if(counts[arrayOfNumbers[i]] === undefined) {
            counts[arrayOfNumbers[i]] = 1;
        } else {
            return true;
        }
    }
    return false;
}
Posted by: Guest on April-09-2020
1

check for duplicates in 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
Posted by: Guest on January-01-2021
1

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)}`);
Posted by: Guest on January-09-2021
0

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)}`);
Posted by: Guest on January-09-2021

Code answers related to "find duplicate values in array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language