Posted By

chrisaiv on 04/28/10


Tagged

array as3


Versions (?)

Who likes this?

8 people have marked this snippet as a favorite

adrianparr
socialflasher
jafar
tspitzr
mitien
danhett
retsub79
scottwatkins


AS3: Remove Duplicates in an Array


 / Published in: ActionScript 3
 

URL: http://thinkdiff.net/flash-action-script-30/in-actionscript-3-how-to-remove-duplicates-from-array/

I found this awesome function that allows you to easily remove duplicate values.

  1. function removeDuplicate(arr:Array) : void{
  2. var i:int;
  3. var j: int;
  4. for (i = 0; i < arr.length - 1; i++){
  5. for (j = i + 1; j < arr.length; j++){
  6. if (arr[i] === arr[j]){
  7. arr.splice(j, 1);
  8. }
  9. }
  10. }
  11. }
  12.  
  13. var arr:Array = new Array("a", "b", "a", "c", "d", "b");
  14. removeDuplicate(arr);

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: makeyko on May 18, 2011

When you splice smthg you remove one element, and array became sorter. Then going to the next element in loop you skip one. Try to add j-- after splice, and this will be correct function;)

Posted By: chrisaiv on June 23, 2011

@makeyko, ThankS!

You need to login to post a comment.