1

I came across a basic question of array in javascript of list out the recurring elements or duplicate elements in an array of strings.

So, lets say we have an array, arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']

So now I tried to solve this by below approach:

var duplicate = [];

function duplicateValues (arr) {
   let l = arr.length;
   
   for (let i=0; i<l; i++){
      if(arr[i+1] == arr[i]) {
     duplicate.push(arr[i]);
   }
 }
   return duplicate;
}

duplicateValues(['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']
);

Expected output:

['Ash', 'Bob', 'Dylan']

But I am getting an empty array in the console. What should be the best approach to solve this problem?

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
starun26
  • 7
  • 3
  • 1
    Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – evolutionxbox Mar 20 '23 at 16:59
  • Your solution checks only adjacent items, you must have two loops or sort items first – Konrad Mar 20 '23 at 17:04
  • Actually, my suggestion is sort of the inverse of what you want. Sorry – evolutionxbox Mar 20 '23 at 17:05
  • also can keep a map/object with counts. and only add to the result array if the count becomes 2. would consume more space but complexity now O(n) – cmgchess Mar 20 '23 at 17:11

4 Answers4

0

This should be very simple, look below.

const d = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John'];

const dub = d.filter(x=> d.filter(f=> f ==x).length>1).reduce((a,v)=> {
  if(!a.find(x=> x == v))
      a.push(v)
    return a
},[])
console.log(dub)
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
0

Wouldn't that be easier?

const array = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']

const result = [...new Set(array.filter((e, i, a) => a.indexOf(e, i + 1) !== -1))]

console.log(result)
Konrad
  • 21,590
  • 4
  • 28
  • 64
0

All you need to do is to just check if a map has that key.

const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan', 'Tom', 'John']

let map = new Map();

const dups = arr.reduce((acc, curr) => {
  if (map.has(curr.toLowerCase()) && acc.indexOf(curr) == -1) {
    acc.push(curr)
  } else {
    map.set(curr.toLowerCase(), curr)
  }

  return acc;
}, []);

console.log(dups)
brk
  • 48,835
  • 10
  • 56
  • 78
0

You could use reduce to get the frequency of each name in the array, then filter only those entries that appear with a frequency of 2 or more.

This approach avoids the use of find and indexOf, which would scan over the array an unnecessary number of times.

const arr = ['Ash', 'Bob', 'Jarvis', 'Ash', 'Ash', 'Dylan', 'Bob', 'Dylan','Tom', 'John']

console.log(Object.entries(arr.reduce((a,c)=>
  (a[c]??=0,a[c]++,a),{})).filter(([k,v])=>v>1).map(([k])=>k))
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27