Remove all occurrences of an element in ArrayList
I am trying to remove all occurrences of an element in an arraylist
ArrayList list=new ArrayList();
list.Add("2.2");
list.Add("2.5");
list.Add("2.6");
list.Add("2.2");
list.Add("2.5");
list.Add("2.2");
How to remove all values ββfrom a list from 2.2? I used
list.Remove("2.2")
but it only removes the first occurrence
source to share
Read the docs ArrayList.Remove () , specifically:
Removes the first occurrence of a specific object from the ArrayList.
Why are you using anyway ArrayList
? The items in your example are of the same type, you are better off using List<string>
and then using RemoveAll for example.
List<string> list = new List<string>();
list.add("2.2");
list.add("2.5");
list.add("2.6");
list.add("2.2");
list.add("2.5");
list.add("2.2");
list.RemoveAll(item => item == "2.2");
source to share
New approach
Shared Collections in the .NET Framework replaces a legacy thing ArrayList
that has some design and performance issues.
Here's an example using List<T>
class and List<T>.RemoveAll(Predicate)
:
var list=new List<string>();
list.Add("2.2");
list.Add("2.5");
list.Add("2.2");
// ...
list.RemoveAll(item => item == "2.2");
Deprecated Code Issue
However, there are some cases where we are required to use a legacy class ArrayList
, for example, when working with legacy code.
Moreover, sometimes legacy APIs require working with a single ArrayList instance, in which case we need a fast and reliable method to work.
Filling the gap
Here's an implementation of the RemoveAll extension that fills in the gap:
public static class ArrayListExtensions
{
/// <summary>
/// Removes all the elements equal to <paramref name="sample"/>.
/// </summary>
/// <param name="list">List to remove from.</param>
/// <param name="sample">Element to remove.</param>
/// <returns>Returns the number of removed elements.</returns>
public static int RemoveAll(this ArrayList list, object sample)
{
if (list == null)
throw new ArgumentNullException("list");
var n = list.Count;
var nextOut = 0;
var nextIn = 0;
for (; nextIn < n && nextIn == nextOut; nextIn++)
{
var token = list[nextIn];
if (!Equals(token, sample))
{
nextOut++;
}
}
for (; nextIn < n; nextIn++)
{
var token = list[nextIn];
if (!Equals(token, sample))
{
list[nextOut] = token;
nextOut ++;
}
}
if (nextOut < list.Count)
{
var toRemove = list.Count - nextOut;
list.RemoveRange(nextOut, toRemove);
return toRemove;
}
return 0;
}
}
This method makes only the necessary basic API calls ArrayList
, works in place and has no additional complexity beyond what's really needed, which is O (n).
source to share
Description is ArrayList.Remove
clear:
Removes the first occurrence of a specific object from the ArrayList. (emphasis mine)
You can do it:
while (list.Contains("2.2"))
{
list.Remove("2.2");
}
source to share
Using a list is the best choice as described above. But if you insist on using it ArrayList
, you can do something like this.
ArrayList list = new ArrayList();
list.Add("2.2");
list.Add("2.2");
list.Add("2.5");
list.Add("2.6");
list.Add("2.2");
list.Add("2.5");
do{
list.Remove("2.2");
} while (list.Contains("2.2"));
may be another way.
list.Sort();
int i = list.LastIndexOf("2.2");
int j = list.IndexOf("2.2");
list.RemoveRange(j,i-j+1);
source to share