Remove items from two lists based on another list

I have three lists. List1

contains some lines. List2

contains some numbers (uint in my case, although not very important), and always has the same number of elements as the first. List3

always has fewer elements and also has some strings. Note that it List1

contains all the elements it List3

has (as well as other strings).

What I want to do is remove all items from List1

that are included in List3

. However, I also want to remove items from List2

like this: If (for example) the List1

5th item List2

needs to be removed , then the 5th item from needs to be removed. If the 12th item is List1

to be removed, then the 12th item from List2

must be removed, etc.

I can remove items from List1

using this code: List1.RemoveAll(x => List3.Contains(x))

. However, I am not very familiar with the method RemoveAll

and I am not sure how to remove the items I want from List2

, in the way I described.

Any ideas?

+3


source to share


3 answers


You can navigate backward through the list, because then you can remove items from the end of the list, for example:



for (var i = list1.Count - 1; i >= 0; i--)
{
    if (list3.Contains(list1[i]))
    {
        list1.RemoveAt(i);
        list2.RemoveAt(i);
    }
}

      

+4


source


As suggested, a simple loop and index lookup:

 list3.ForEach(x =>
   {  
      var index = list1.FindIndex(list3.Contains);
      list2.RemoveAt(index);
      list1.RemoveAt(index);
   }

      



But if the first list contains duplicates:

 var query = list1.Where(list3.Contains).Select((value, index) => new {index}).ToList();
     query.ForEach(x =>
        {
           list2.RemoveAt(x.index);
           list1.RemoveAt(x.index);
        });

      

+1


source


Maybe List1

u List2

can be combined in Dictionary<string, int>

if they are for communication? Then deleting the data from List1

and List2

will mean simply deleting KeyValuePair

. But of course List1

it shouldn't have any duplicates, otherwise the dictionary will have duplicate keys.

If a Dictionary

makes sense semantically (with List1

containing keys and List2

containing corresponding values), here's the implementation:

Dictionary<string, uint> dictionary = List1.ToDictionary(key => key, key => List2[List1.IndexOf(key)];
Dictionary<string, uint> result = dictionary.Where(keyValuePair => !List3.Contains(keyValuePair.Key));

      

A similar implementation can be used if it List2

must contain keys, but List1

must contain values:

Dictionary<uint, string> dictionary = List2.ToDictionary(key => key, key => List1[List2.IndexOf(key)];
Dictionary<uint, string> result = dictionary.Where(keyValuePair => !List3.Contains(keyValuePair.Value));

      

The presence of List1

and is List2

so strongly related (both lists have the same number of elements; if an element is removed from List1

, then the corresponding element must be removed from List2

), suggests that a Dictionary

might be appropriate. (But this is just a suggestion. If it Dictionary

doesn't work, I would use a simple and efficient solution based on Dzienny loops.)

+1


source







All Articles