Answers for "duplicate values from array js"

1

javascript create array with repeated values

Array(5).fill(2)
//=> [2, 2, 2, 2, 2]
Posted by: Guest on February-17-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 "duplicate values from array js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language