Code This #4: Remove Duplicates

Interview Question #4:

Write a function that will remove duplicate in an array.?❓ You can get a variation of this question as Get unique characters from a list.

If you need practice, try to solve this on your own. I have included 2 potenti…


This content originally appeared on DEV Community and was authored by Let's Code

Interview Question #4:

Write a function that will remove duplicate in an array.?❓ You can get a variation of this question as Get unique characters from a list.

If you need practice, try to solve this on your own. I have included 2 potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark ? even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Solution #1: ES6 Set

function removeDuplicates(array) {
  return [...new Set(array)]
}

Solution #2: Object

function removeDuplicates(array) {
  const map = {}

  for (const char of array) {
    if (map[char]) {
      map[char]++
    } else {
       map[char] = 1
    }
  }

  return Object.keys(map)
}

In case you like a video instead of bunch of code ??

Happy coding and good luck if you are interviewing!


This content originally appeared on DEV Community and was authored by Let's Code


Print Share Comment Cite Upload Translate Updates
APA

Let's Code | Sciencx (2021-08-29T16:21:06+00:00) Code This #4: Remove Duplicates. Retrieved from https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/

MLA
" » Code This #4: Remove Duplicates." Let's Code | Sciencx - Sunday August 29, 2021, https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/
HARVARD
Let's Code | Sciencx Sunday August 29, 2021 » Code This #4: Remove Duplicates., viewed ,<https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/>
VANCOUVER
Let's Code | Sciencx - » Code This #4: Remove Duplicates. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/
CHICAGO
" » Code This #4: Remove Duplicates." Let's Code | Sciencx - Accessed . https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/
IEEE
" » Code This #4: Remove Duplicates." Let's Code | Sciencx [Online]. Available: https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/. [Accessed: ]
rf:citation
» Code This #4: Remove Duplicates | Let's Code | Sciencx | https://www.scien.cx/2021/08/29/code-this-4-remove-duplicates/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.