October 25, 2006 6:50 pm
public
ArrayList RemoveDups(ArrayList items){
ArrayList noDups =
new ArrayList(); foreach(string strItem in items){
if (!noDups.Contains(strItem.Trim())){
noDups.Add(strItem.Trim());
}
}
noDups.Sort();
return noDups;}
Handy!
Posted by Infinite Loop Development Ltd
Categories: Uncategorized
Tags:
Mobile Site | Full Site
Get a free blog at WordPress.com Theme: WordPress Mobile Edition by Alex King.
Very cool – thanks
LikeLike
By Brian on December 21, 2010 at 5:42 pm
thanks a ton..!!
LikeLike
By nawfal on April 8, 2011 at 12:20 pm
I can’t get this to work – sorry, but I am a noob in my first c# class.
Here is what I have (please note that I switched out “noDups” with “trimmage” – I was already using the name noDups throughout my code for my initial array list. I need each item to show up only twice in a random order:
ArrayList noDups = new ArrayList();
public void RandomOrder(ArrayList arrList)
{
Random r = new Random();
for (int cnt = 0; cnt < arrList.Count; cnt++)
{
object tmp = arrList[cnt];
int idx = r.Next(arrList.Count – cnt) + cnt;
arrList[cnt] = arrList[idx];
arrList[idx] = tmp;
}
}
public ArrayList RemoveDups(ArrayList noDups)
{
ArrayList trimmage = new ArrayList();
foreach(string strItem in noDups)
{
if (!trimmage.Contains(strItem.Trim()))
{
trimmage.Add(strItem.Trim());
}
}
return trimmage;
}
public void randomAssign()
{
noDups.Add("A");
noDups.Add("A");
noDups.Add("B");
noDups.Add("B");
noDups.Add("C");
noDups.Add("C");
noDups.Add("D");
noDups.Add("D");
noDups.Add("E");
noDups.Add("E");
noDups.Add("F");
noDups.Add("F");
RandomOrder(noDups);
RemoveDups(noDups);
LikeLike
By Garrett Moeller on August 5, 2011 at 2:15 pm
Please note that a problem with this is that I need two instances of each string item….Ex: I need two “A”…..write now, this code is producing more than two!
LikeLike
By Garrett Moeller on August 5, 2011 at 2:55 pm
Better use a LINQ expression:
http://stackoverflow.com/questions/9673/remove-duplicates-from-array
LikeLike
By Lelala on November 2, 2012 at 8:20 pm