Posted By

Leech on 07/21/06


Tagged

arrays


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jkochis
shachi


Remove Duplicated v1.1


 / Published in: JavaScript
 

URL: http://jsfromhell.com/array/remove-duplicated

Removes duplicated values on an array. Created: 2005.08.13 - Modified: 2005.11.19

  1. /*
  2. **************************************
  3. * Remove Duplicated Function v1.1 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7. removeDuplicated = function(a, s){
  8. var p, i, j;
  9. if(s) for(i = a.length; i > 1;){
  10. if(a[--i] === a[i - 1]){
  11. for(p = i - 1; p-- && a[i] === a[p];);
  12. i -= a.splice(p + 1, i - p - 1).length;
  13. }
  14. }
  15. else for(i = a.length; i;){
  16. for(p = --i; p > 0;)
  17. if(a[i] === a[--p]){
  18. for(j = p; --p && a[i] === a[p];);
  19. i -= a.splice(p + 1, j - p).length;
  20. }
  21. }
  22. return a;
  23. };

Report this snippet  

You need to login to post a comment.