Last Updated: February 25, 2016
·
755
· gnclmorais

Simplest ‘removeDuplicates’ in JavaScript Arrays

Other question I’ve found a lot in job interview questions, and this is the simplest implementation I’ve found:

[…].filter(function (val, idx, arr) {
    var firstIdx = arr.indexOf(val);
    return (idx === firstIdx) || (firstIdx === arr.lastIndexOf(val));
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside