Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

Flex How to remove Duplicates from an Array

Posted on September 28, 2010 | Comments Off on Flex How to remove Duplicates from an Array

If you have an Array and you want to get rid of duplicates from the array, here is a little function you can order online levitra use to achieve the same.

private function removeDuplicates(arr:Array):Array
{
	var   currentValue:String   = "";
	var tempArray:Array = new Array();
	arr.sort(Array.CASEINSENSITIVE);
	arr.forEach(
		function(item:*,     index:uint, array:Array):void 
		{
			if (currentValue   != item) 
			{
				tempArray.push(item);
				currentValue= item;
			}
		}
	);
	return   tempArray.sort(Array.CASEINSENSITIVE);
}

Basically, all its doing is making use of the forEach function built in Flex that runs a custom function for every item in the Array. This function then sorts the data alphabetically, checks each value with the previous value and if they are not same, adds the unique element to the new tempArray and thats what gets returned.


Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Comments are closed.