How to get the unique values of an array in Node.js

1 Answer

0 votes
function arrayUnique(arr) {
    const result = [];
    
    arr.forEach(val => {
        if (!result.includes(val)) {
            result.push(val);
        }
    });
    
    return result;
}

const arr = [1, 2, 1, 1, 3, 3, 1, 4, 4, 5, 5, 5, 5, 2, 2, 6, 7, 7, 8, 9, 8];
const result = arrayUnique(arr);

result.forEach(val => console.log(val));




/*
run:

1
2
3
4
5
6
7
8
9

*/

 



answered Feb 18, 2025 by avibootz

Related questions

1 answer 73 views
2 answers 123 views
1 answer 131 views
2 answers 161 views
1 answer 131 views
...