Skip to Main Content

Programming Languages & Frameworks

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

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 ***

Comments
Post Details
Added on Aug 17 2023
0 comments
123 views