02 Apr 2009 @ 10:22 AM 
 

Find all unique values from array or collection

 

I needed a quick/simple method to iterate through a collection of data and pull out just the unique values.

So if you had a collection that looked like

<data>

<info name=”B” />
<info name=”C” />

<info name=”B” />

<info name=”A” />

<info name=”B” />

<info name=”C” />

<info name=”C” />

<info name=”A” />

<info name=”B” />

<info name=”C” />

<info name=”B” />

</data>

Then you only have 3 unique values (A, B & C).

So I came up with a method that is effectively a single pass through the data.

It uses a dictionary, and the value of the objects inside the AC for the key.

So if you have a dictionary variable dict, then if you have a value of ‘XX’ and you use that as the key (dict[‘XX’] = ‘XX’) then if you have another value of ‘XX’ and assign that to dict[‘XX’] = ‘XX’ then the previous entry gets overwritten giving you a set of unique values.

Its just a case of then getting the value out of the dictionary and back into an AC. (dictionary’s are not Bindable like AC’s)

 

//takes an AC and the filters out all duplicate entries
public function getUniqueValues (collection : ArrayCollection) : ArrayCollection {
	var length : Number = collection.length;
	var dic : Dictionary = new Dictionary();
 
	//this should be whatever type of object you have inside your AC
	var value : Object;
	for(var i : Number = 0; i &lt; length; i++){
		value = collection.getItemAt(i);
		dic[value] = value;
	}
 
	//this bit goes through the dictionary and puts data into a new AC
    var unique = new ArrayCollection();
    for(var prop :String in dic){
    	unique.addItem(dic[prop]);
    }
    return unique;
}

Tags Tags: , , , ,
Categories: Flex
Posted By: Kenneth
Last Edit: 25 May 2009 @ 01 01 PM

EmailPermalink
 

Responses to this post » (7 Total)

 
  1. Chris Lyles says:

    Thanks for taking the time to publish your handy work…it’s been very helpful.

  2. VANESSA says:

    thanks for this code. It will definitely be useful innumberable times for me…
    Good job and keep blogging…

  3. Suma Gowda says:

    Thank You :-)

  4. Bendy says:

    I do believe the dictionary hash uses AS3′s built-in “equality”: comparing by reference. You only get unique objects by reference.

    If you want unique objects by equality, you should use ObjectUtil.compare and a double loop, checking each item against the unique items found so far. This is something like O(n^2).

  5. Kenneth says:

    As long as you remember the following when using the keys then it works fine and is much quicker than a double loop.
    The Dictionary class lets you create a dynamic collection of properties, which uses strict equality (===) for key comparison. When an object is used as a key, the object’s identity is used to look up the object, and not the value returned from calling toString() on it. Primitive (built-in) objects, like Numbers, in a Dictionary collection behave in the same manner as they do when they are the property of a regular object.

  6. Brian Bishop says:

    Hi

    Nice post. Heres a helpful addition to the function that I needed. If you have an array of objects (any type of objects), you can pass in which property of the objects you wish to target.


    /**
    * Get the unique values stored within an arrayCollection
    *
    * @param The arrayCollection to search
    * @param The property value of the arrayCollection's objects to target
    * @return an arrayCollection of unique items;
    */
    public function getUniqueValues (collection:ArrayCollection, propertyName:String):ArrayCollection {

    var length:Number = collection.length;
    var dict:Dictionary = new Dictionary();

    //this should be whatever type of object you have inside your AC
    var obj:Object;
    for(var i:int = 0; i < length; i++){
    obj = collection.getItemAt(i);

    dict[obj[propertyName]] = obj[propertyName];
    }

    //this bit goes through the dictionary and puts data into a new AC
    var unique:ArrayCollection = new ArrayCollection();
    for(var propertyString:String in dict){
    unique.addItem(dict[propertyString]);
    }
    return unique;
    }

  7. harishvardhan says:

    i have a arraycollection in which each element is a dictionary
    for ex: Item 1: Fontstyle:Italic
    1:46 item 2:Fontstyle :bold
    item3:Underline :true
    in this array collection i want to remove the fontstyle attributed items and add it to a new dictionary where in it contains only the unique values

    how to do this?

    can you email me please?harishvardhan@ymail.com

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

 


\/ More Options ...
Change Theme...
  • Users » 1
  • Posts/Pages » 45
  • Comments » 54
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight