You can use the array methods find and lastIndexOf/indexOf to do this in a more direct manner:
find
lastIndexOf
indexOf
return array.find((item) => array.indexOf(item) === array.lastIndexOf(item))
find has the added advantage over filter that it stops iterating after the first match. Sorting the array will only work for numbers, strings or objects that can be coerced to either.
filter
Great solution for optimizing the code, thanks bro! ππ₯
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can use the array methods
find
andlastIndexOf
/indexOf
to do this in a more direct manner:find
has the added advantage overfilter
that it stops iterating after the first match. Sorting the array will only work for numbers, strings or objects that can be coerced to either.Great solution for optimizing the code, thanks bro! ππ₯