Remove Duplicate Values from Array


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

Simple function that filters out any duplicate items in an array.


Copy this code and paste it in your HTML
  1. var unique = function(origArr) {
  2. var newArr = [],
  3. origLen = origArr.length,
  4. found,
  5. x, y;
  6.  
  7. for ( x = 0; x < origLen; x++ ) {
  8. found = undefined;
  9. for ( y = 0; y < newArr.length; y++ ) {
  10. if ( origArr[x] === newArr[y] ) {
  11. found = true;
  12. break;
  13. }
  14. }
  15. if ( !found) newArr.push( origArr[x] );
  16. }
  17. return newArr;
  18. }
  19.  
  20. var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie', 'patty', 'damon', 'zach', 'joe'];
  21. myarray = unique(myarray);
  22. alert(myarray.join(', '));

URL: http://jsfiddle.net/ARnL3/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.