This is one of the questions that I used to ask in interviews. I was also asked this question initially when I started my career as a software engineer.
It’s a very simple problem yet surprisingly many fails to answer it. I know because I have asked many candidates and not all of them were able to answer correctly.
There are many ways to do this. So let’s take a look at the different ways to do it one by one:
Using Set
(My favorite way)
It is the shortest way to achieve this:
Using indexOf()
and loop
Using indexOf()
and filter
Using forEach()
and include()
I hope this was useful for you!
Thanks for reading!