Skip to content
Advertisement

Javascript remove all occurrence of duplicate element, leaving the only one that is unique

I want to remove elements that occurr more than once and get the unique element. The array always has 3 elements. Lets say i have an array [2,3,2], then I need to get 3 which is only unique in the array(removing both 2s, because they occur more than once).

I have tried with following code, but surely it doesnot work as expected.

JavaScript

Thanks!

Advertisement

Answer

This should do the trick:

JavaScript

To check if a specific item is unique in the array, it just checks if the first index it’s found at, matches the last index it’s found at.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement