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
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;
}
7 Comments
fsbmain
simpler, but slower. It has complexity ~O(N^2), while @rawry one is ~O(N). Better solution would be without using indexOf at all.
Florian Salihovic
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.
Mike Petty
@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).
fsbmain
@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.
Brian
2*O(N) = O(2N) = O(N)
|
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
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
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
Antoine Thiry
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.