Posted By

brianyang on 02/01/11


Tagged


Versions (?)

Remove Duplicates from Array


 / Published in: JavaScript
 

  1. var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
  2. var sorted_arr = arr.sort(); // You can define the comparing function here. JS default uses a crappy string compare.
  3. var results = [];
  4. for (var i = 0; i < arr.length - 1; i += 1) {
  5. if (sorted_arr[i + 1] == sorted_arr[i]) {
  6. results.push(sorted_arr[i]);
  7. }
  8. }
  9.  
  10. alert(results);

Report this snippet  

You need to login to post a comment.