Ali Chbinou
Ali Chbinou

Reputation: 93

Finding duplicate elements in Javascript arrays

I have an array, and I want to find which element is there twice.

I thought of making a new array, for each element in the original array, without that element. This array contains the same elements, but after slicing the one we are dealing with. Then we'd try to find it in this new array: so if it exists twice in the original array, it'd be there and it would return "true", and if not, it would not be in the new array and it would return "false", based on my code.

let arr = ["stephanie", "alex", "steven", "alex"]

function double(name) {
  arr.forEach((name, index) => {
    let newarr = arr.slice(index);
    console.log(newarr);
    const searchedelement = newarr.find(name === nom);
    console.log(searchedelement);
  })
}

console.log(double(name))

Upvotes: 3

Views: 144

Answers (4)

Naga Sai A
Naga Sai A

Reputation: 10975

To achieve expected result, use below option of usind reduce to loop through only once

  1. Loop through using reduce and push to object of accumulator
  2. Check whether property exists or not and push to duplicate array
  3. Return duplicate array

let arr = ["stephanie", "alex", "steven", "alex"]

function double(arr){
  const dup =[]
  arr.reduce((acc, v) => {
    acc[v] = acc[v] ? dup.push(v) : 1;
    return acc
  }, {});
  return dup
}
console.log(double(arr))

Option 2: Just to check whether duplicate exists or not, just use Set and compare length

let arr = ["stephanie", "alex", "steven", "alex", "steven"]

function double(arr){
   const uniq = [...new Set(arr)]
   return arr.length !== uniq.length
}

console.log(double(arr))

Upvotes: 1

claasic
claasic

Reputation: 1050

If there is only one double in the whole array use the following:

let arr = ["stephanie", "alex", "steven", "alex"]
    res = arr.filter(v => arr.indexOf(v) !== arr.lastIndexOf(v))[0];

console.log(res)

If there are different doubles to be expected use this:

let arr = ["stephanie", "alex", "steven", "alex","steven"]
    res = [...new Set(arr.filter(v => arr.indexOf(v) !== arr.lastIndexOf(v)))];

console.log(res)

Important: Both snippets work for any kind of repetition so if you want to check for exactly an occurrence of two this needs to be changed!

If you want only doubles that are actual doubles but not triples or anything consider this:

let arr = ["stephanie", "alex", "steven", "alex","steven", "alex"]
    res = Object.entries(arr.reduce((a,c) => {a[c] = (a[c] || 0 ) + 1; return a},{}))
                .filter(v => v[1] == 2).map(v => v[0]);

console.log(res)

Upvotes: 2

Manish Khedekar
Manish Khedekar

Reputation: 402

You may try out as,

let arr = ["stephanie", "alex", "steven", "alex"];

function isDouble(arr) {
  let isDouble = false;
  for(let el of arr) {
    if(arr.filter(element => element != el).length === arr.length-2){
        isDouble = true;
        break;
    }
  };
  return isDouble;
};

console.log(isDouble(arr));

Upvotes: 0

Kévin Bibollet
Kévin Bibollet

Reputation: 3623

You can filter your array by checking how many times values are found in it.

Then, construct an Set object to eliminate all the duplicates.

const arr = ['joe', 'steph', 'john', 'joe', 'thomas', 'john'];

console.log(
  [...new Set(arr.filter(e => arr.filter(s => s === e).length > 1))]
);

Upvotes: 0

Related Questions