Skip to content

Instantly share code, notes, and snippets.

@bferronato
Last active November 19, 2016 11:49
Show Gist options
  • Save bferronato/f7f526d5d6bee1366619398cc5577357 to your computer and use it in GitHub Desktop.
Save bferronato/f7f526d5d6bee1366619398cc5577357 to your computer and use it in GitHub Desktop.
/**
* Torna os elementos de um array unico, incrementando os que forem iguais
*/
Array.prototype.oneOfAKind = function() {
for(var i = 1; i < this.length; i++) {
if(this[i-1] >= this[i]) {
this[i] = this[i] + 1;
return this.oneOfAKind();
}
}
return this;
}
// Use this way
var result = [1,1,2,2,4,4,4,4,5].oneOfAKind();
console.log(result.join());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment