Remove Duplicate Numbers in an Array


/ Published in: JavaScript
Save to your folder(s)

from this page: http://dreaminginjavascript.wordpress.com/2008/08/22/eliminating-duplicates/


Copy this code and paste it in your HTML
  1. var arr=[9, 7, 1, 9, 2, 3, 7, 4, 5, 4,7],
  2. i,
  3. arrayLength=arr.length,
  4. out=[],
  5. obj=[];
  6.  
  7. for (i=0; i < arrayLength; i++) {
  8. obj[arr[i]]=0;
  9. }
  10.  
  11. for (i in obj) {
  12. out.push(i);
  13. }
  14.  
  15. alert(out);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.