Filter non-duplicate objects from collection I need to filter an array of form controls to check for duplicate values. I have written the following algorithm to identify and return an array of the controls which have are duplicates. I have not seen this indexOf based approach in any of the sample duplicate checkers one finds in Google. Can anyone here spot a flaw in my code?

Testing codepen.

// select all keys
const duplicateKeys = values.map((current) =&gt; current.name);
// filter duplicate keys
const filteredDuplicateKeys = duplicateKeys.reduce((prev, name, index, names) =&gt; {
  if (names.indexOf(name) !== names.indexOf(name, index)) {
    if (!prev.includes(name)) return prev.concat(name);
  }
  return prev;
}, []);
// filter original array based on identified duplicate keys
const duplicates = values.reduce((prev, current) =&gt; filteredDuplicateKeys.indexOf(current.name) &gt; -1 ? prev.concat(current) : prev, []);
The important flaw in your code is runtime complexity.  reduce, indexOf and includes all have runtimes that are linear (O(n)) in the size of the array.  You're running the latter two once for every iteration of reduce, giving a quadratic (O(n2)) runtime.  Doubling the length of the input will quadruple the running time.

A secondary flaw is excess regular human-brain complexity: it's more lines of code and data structures than are needed.  

The typical approach involves a single hash table to count duplicates and just two lines of code: one to count, one to filter:

var countKeys = {};
values.forEach( value =&gt; countKeys[value.name] = (countKeys[value.name] || 0) + 1 )
const duplicates = values.filter( value =&gt; countKeys[value.name]&gt;1 );




const values = [
  {
    name: 'Duplicate 1',
    value: [1,2,3],
  },
  {
    name: 'Duplicate 2',
    value: [1,2,3],
  },
  {
    name: 'Duplicate 1',
    value: [1,2,34],
  },
  {
    name: 'Duplicate 2',
    value: [1,2,3],
  },
  {
    name: 'Duplicate 2',
    value: [1,2,3],
  },
  {
    name: 'Not Duplicate',
    value: [1,2,3],
  },
];


var countKeys = {};
values.forEach( value =&gt; countKeys[value.name] = (countKeys[value.name] || 0) + 1 )
const duplicates = values.filter( value =&gt; countKeys[value.name]&gt;1 );
console.log(duplicates)
&lt;p&gt;