Skip to Main Content

Programming Languages & Frameworks


For appeals, questions and feedback, please email

Remove duplicate values from a JavaScript array to get all unique values

user-mxzr4Aug 17 2023 — edited Aug 18 2023

Hello everyone

I'm attempting to find a solution for the duplicates in an array issue. I need to ensure that each integer in an array I have is distinct. The code sample below, which I got online, works well up until the array contains a zero.

Can someone please tell me what's wrong with the prototype script so I can learn from it?

Array.prototype.getUnique = function() {
var o = {}, a = [], i, e;
for (i = 0; e = this[i]; i++) {o[e] = 1};
for (e in o) {a.push (e)};
return a;

Question reference from ***Moderator action: removed link to an outside page as this is considered spam ***

Post Details
Added on Aug 17 2023