Remove Duplicates from an Array AS3

I recently needed function that would remove duplicate values from an array. After looking around online and modifying what I found to fit my needs I came up with the following:

var arr:Array = ["a", "b", "c", "a", "d", "a", "a"];

removeDuplicates(arr);

trace(arr);

function removeDuplicates(target:Array):void
{
	for(var i:uint=target.length; i>0; i--)
	{
		if(target.indexOf(target[i-1]) != i-1)
		{
			target.splice(i-1, 1);
		}
	}
}

If you are searching for a way to do this, I hope this helps!

2 thoughts on “Remove Duplicates from an Array AS3”

  1. How would you like this simpler version?

    var uniqArray:Array = arr.filter(function(item:String, index:int, array:Array):Boolean {
    return array.indexOf(item) == index;
    });

    That is for array of strings, can use item:* for arbitrary data.

Leave a Reply

Your email address will not be published. Required fields are marked *