Remove duplicates from Array


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // removes all duplicate items from an array
  2.  
  3. /**
  4. * @method removeDuplicates
  5. * @description removes duplicate items from the array
  6. * @param haystack:Array - the array from which to remove any duplicates
  7. */
  8. public static function removeDuplicates(haystack:Array):Array{
  9.  
  10. var dict:Dictionary = new Dictionary( true );
  11. var output:Array = new Array( );
  12. var item:*;
  13. var total:int = haystack.length;
  14. var pointer:int = 0;
  15. for(pointer; pointer < total ; pointer++) {
  16. item = haystack[pointer];
  17. if(dict[item] == undefined) {
  18. output.push( item );
  19. dict[item] = true;
  20. }
  21. }
  22. return output;
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.