3

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))
1
  • 1
    Note: you're not returning anything from double for you to log. Commented Jun 27, 2019 at 13:22

4 Answers 4

2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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))

Comments

0

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))]
);

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.