0

I have a Dictionary set up like so:

private Dictionary<string, List<string>> Directory = new Dictionary<string, List<string>>();

I am needing to iterate over every Key in the Dictionary and remove all occurrences of a string within each of the lists.

I believe I can do so with a series of for/for-each loops, possibly by creating a new list for each without the string that I am trying to remove but I feel this may become inefficient when the Dictionary grows in size.

Are there any methods that can make this more efficient?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
cillsley
  • 37
  • 8

4 Answers4

2

I would use a traditional foreach loop

var strToRemove == "remove me!";
foreach (var listObj in dic.Values)
{
    listObj.RemoveAll(x => x.Equals(strToRemove ,StringComparison.OrdinalIgnoreCase));
}
ilkerkaran
  • 4,214
  • 3
  • 27
  • 42
1

You can use RemoveAll() in a foreach loop to remove all occurence in all lists of a specified string :

foreach (var val in Directory.Values)
{
    var numberOfElementsRemoved = val.RemoveAll(x => x.ToLower() == StringToRemove.ToLower());
}
Cid
  • 14,968
  • 4
  • 30
  • 45
0

You can use the Where statement to look up the pair that has the value you need to remove... then update the value of that pair.

    Dictionary<string, List<string>> Directory = new Dictionary<string, List<string>>();
    Directory.Add("1", new List<string>() { "alpha", "bravo", "charlie" });
    Directory.Add("2", new List<string>() { "delta", "echo", "foxtrot" });
    Directory.Add("3", new List<string>() { "golf", "charlie", "India" });

    string stringToRemove = "charlie";
    Directory.Where(x => x.Value.Contains(stringToRemove))
                .ToList()
                .ForEach(x => x.Value.Remove(stringToRemove));

After the execution, "charlie" is gone from all dictionary's pairs.

As per Cid's suggestion, you can also use RemoveAll(lambda) if you dont have a distinct list.

Directory.Where(x => x.Value.Contains(stringToRemove))
           .ToList()
           .ForEach(x => x.Value.RemoveAll(x => x.Equals(strToRemove ,StringComparison.OrdinalIgnoreCase)));
Jawad
  • 11,028
  • 3
  • 24
  • 37
0

Solution:

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
        dictionary.Add("Test1", new List<string>() { "Test1", "Test2", "Test3" });
        dictionary.Add("Test2", new List<string>() { "Test2", "Test3", "Test1" });
        dictionary.Add("Test3", new List<string>() { "Test3", "Test2", "Test1" });
        dictionary.RemoveStringFromEveryList("Test1");
    }
}

public static class Extensions
{
    public static void RemoveStringFromEveryList(this Dictionary<string, List<string>> dictionary, string wordToRemove)
    {
        foreach(var keyValue in dictionary)
        {
            keyValue.Value.RemoveAll(x => x == wordToRemove);
        }
    }
}

I would use an extension method for a better usage and readability of your code.

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32