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.
Asked
Active
Viewed 2,948 times
5 Answers
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;
}

Saša
- 4,416
- 1
- 27
- 41

Florian Salihovic
- 3,921
- 2
- 19
- 26
-
1simpler, but slower. It has complexity ~O(N^2), while @rawry one is ~O(N). Better solution would be without using _indexOf_ at all. – fsbmain Aug 01 '14 at 10:52
-
2There 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. – Florian Salihovic Aug 01 '14 at 11:03
-
@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 Aug 01 '14 at 19:41
-
@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. – fsbmain Aug 04 '14 at 07:26
-
2*O(N) = O(2N) = O(N) – Brian Nov 13 '14 at 00:08
-
I'm all for iterative development (build only what you need, etc), but ```O(n^2)``` for something that naively is ```O(n)``` is pretty grossly inefficient. I also think there's a typo: shouldn't ```removeDublicatesInArray``` be ```removeDuplicatesInArray```? – Adam Parkin Nov 25 '14 at 19:25
-
this doesn't even work. if the array has 2 same elements it will remove both of them – deloki Jan 12 '22 at 20:09
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;
}

George Profenza
- 50,687
- 19
- 144
- 218

Roman Kolpak
- 1,942
- 21
- 25
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.

Crabar
- 1,829
- 1
- 14
- 26
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());

zawhtut
- 8,335
- 5
- 52
- 76
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;
}

Nizar
- 1
-
1While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Antoine Thiry Dec 28 '18 at 14:43