Coding Challenge: Find out duplicates from given Array

Question: Find out the duplicates from a given array and return it.

Solution: To solve this problem, we will use “map” data structure and check if “Key” exists within map or not. If the “key” does exist then add that key to result array as duplicate entry otherwise we will add that into map.

Watch this Tutorial:

Let’s add this logic into executable code!

const find_duplicate_in_array = (arra1) => {
   let i,
       len = arra1.length,
       result = [],
       obj = {};
   for (i = 0; i < len; i++) {
       if (!obj.hasOwnProperty(arra1[i])) {
           obj[arra1[i]] = 1;
       }
       else {
           result.push(arra1[i]);
       }
   }
   result = [...new Set(result)]
   return result;
}
Sample Data:

const arr = [1, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6];
console.log(find_duplicate_in_array(arr));

Here you can run and play the code:

The tutorial and coding challenge above could be very helpful if you are preparing for Software Developer, Software engineer or Technical Engineering role. Big Tech companies like Amazon, Facebook and others ask this question during their tech screening and coding interview.

If you like the video tutorial Share, like the video and Subscribe to our Channel.