DEV Community

Discussion on: What’s your alternative solution? Challenge #29

Collapse
 
themobiledev profile image
Chris McKay • Edited

So, here's my solution:

var ar = getDistinctElements([1, 2, 3, 6, -1, 2, 9, 7, 10, -1, 100]);
println(ar);
function getDistinctElements(ar = []) {
    return [...new Set(ar)];
}

So, just by way of explanation, Set is an object that only accepts distinct values. Using the spread operator (...) inside array brackets converts the set back into an array.