Find all unique values from array or collection

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





  
  
  
  
  
  
  
  
 
 

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 < length; i++){
		value = collection.getItemAt(i);
		dic[value] = value;
	}

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

[ad name="ad-1"]

9 Replies to “Find all unique values from array or collection”

  1. 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).

    1. 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.

  2. 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;
    }

  3. 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 [email protected]

  4. Hi
    The solution is ok, but how can I ignore case sensitivity?
    I mean either value are in capital or small the value should ignore case sensitivity.

    Thanks in Advance

Leave a Reply to Kamran Aslam Cancel reply

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

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.