• 0

Remove duplicates from the given array

Problem description :

Write a function that removes duplicates entries from a given array.

Input : An Array // ['Heart5', 'Diamond6', 'Club7', 'Spade8', 'Diamond6', 'Club7']
Output : An Array // ['Heart5', 'Diamond6', 'Club7', 'Spade8']

Logic :

  • Create a hash table called uniqueCards with key being an element from an array and value being true or false.
  • Iterate through array
    • If an element does not exist in the uniqueCards then add the element and set its value to true.
  • Return the keys from the uniqueCards.

Solution 1:

Naive solution



Solution 2 :

Using Array.prototype.filter()



Solution 3 :

Using Array.prototype.filter()



Solution 4 :

Using Array.prototype.forEach()