1

I have an ArrayCollection of a list of usernames and user id's. In this list there are duplicates that I need to remove. I've searched the internet and while there are a lot of example of this using Arrays, I can't find any clear examples using ArrayCollection's.

5 Answers 5

6

The should be simpler then the other solution.

function removeDuplicatesInArray(val:*, index:uint, array:Array):Boolean {
  return array.indexOf(val) == array.lastIndexOf(val);
}

function removeDuplicatesInCollection(collection:ArrayCollection):ArrayCollection {
  collection.source = collection.source.filter(removeDuplicatesInArray);

  return collection;
}
Sign up to request clarification or add additional context in comments.

7 Comments

simpler, but slower. It has complexity ~O(N^2), while @rawry one is ~O(N). Better solution would be without using indexOf at all.
There is always room for optimisation. I wanted to provide a simple solution which is easy to understand. I don't think it's productive to provide complicated solutions for non complicated questions. If it is too slow, we'll make it faster. But if filtering for unique items is a problem for someone, I think it's wrong to offer code the asker will have trouble to understand. Just my 2 cents.
@fsbmain - That would depend on the requirements now wouldn't it? Just because you can build a mansion doesn't mean you should when a tree fort will do. 10,000 elements? Sure... 100? probably not. Also, that solution is at best O(N) and at worst 2O(N).
@Mike Petty and Florian I agree with both of you, there isn't only white and only black, everything is gray ) The rawry solution may have more memory consumption for instance, but in general I prefer faster solution, nobody knows how many elements does author filter and how often. Mike, the filter method it's self is O(N) and indexOf + lastIIndexOf is 2*O(N), so final complexity is O(N)*2O(N) ~ 2O(N^2) in worth case.
2*O(N) = O(2N) = O(N)
|
4

Here's what I found after quick googling.

//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 : int= 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 = new ArrayCollection();
    for(var prop:String in dic){
        unique.addItem(dic[prop]);
    }
    return unique;
}

Comments

0

If you find solutions for the array you can do the same with the ArrayCollection. You can change arrayCollection.source and arrayCollection will be changed too. In general, we can assume that ArrayCollection is wrapper for Array.

Comments

0

Array contain a filter function and we can make use of it as following.

            var ar:Array = ["Joe","Bob","Curl","Curl"];
            var distinctData = ar.filter(function(itm, i){
                return ar.indexOf(itm)== i; 
            });

            Alert.show(distinctData.join(","));

Or better yet

            Array.prototype.distinct = function():*
            {
                var arr:Array = this as Array;
                return arr.filter(function(itm, i){
                    return (this as Array).indexOf(itm)== i; 
                },arr);
            };              

            var ar:Array = ["Joe","Bob","Curl","Curl"];
            Alert.show(ar.distinct());

Comments

0
function removeDuplicateElement(_arr:Array):Array{
    //set new Dictionary
    var lDic:Dictionary = new Dictionary();
    for each(var thisElement:* in _arr){
        //All values of duplicate entries will be overwritten
        lDic[thisElement] = true;
    }
    _arr = [];
    for(var lKey:* in lDic){
       _arr.push(lKey);
    }
    return _arr;
}

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.