Array.prototype.unique = function() {
var a = [], // uniques get placed into here
b = 0; // counter to test if value is already in array 'a'
for ( i = 0; i < this.length; i++ ) {
var current = this[i]; // get a value from the original array
for ( j = 0; j < a.length; j++ ) { // loop and check if value is in new array
if ( current != a[j] ) {
b++; // if its not in the new array increase counter
}
}
if ( b == a.length ) { // if the counter increased on all values
// then its not in the new array yet
a.push( current ); // put it in
}
b = 0; // reset counter
}
this.length = 0; // after new array is finished creating delete the original array
for ( i = 0; i < a.length; i++ ) {
this.push( a[i] ); // push all the new values into the original
}
return this; // return this to allow method chaining
}
I'm expecting this to be a slow checker especially because there is no sorting first. I'm interested in improving my sorting abilities etc so I thought I would get an early review.
Array.prototype.unique = function() { return [...new Set(this)]; }
Test:[1,3,4,4,4,3,1,2,5,6,6,7].unique() // [1, 3, 4, 2, 5, 6, 7]
\$\endgroup\$