this code works if string in list2 matches string in list1 and it is time efficient when it comes to larger lists
var firstNotSecond = list1.Except(list2).ToList();
however I'm trying to tweak it so that it would work if string in list2 contains a string in list1. So say if:
--list2 strings:
"john,sally,michael"
"tim,sally,andrew"
"stuart,bill,tom"
--list1 strings:
"sally"
"joe"
--final list would contain only last string from list2 as first two .Contains
"stuart,bill,tom"
I'm looking for the most time/resources efficient way to do this - the straight forward implementation with essentially two nested loops (with or without LINQ) is O(len(list1) * len(list2) ) - ideally I'd like same O(max(len(list1), len(list2))) as with .Except, but anything better than essentially n^2 is welcome.
Here's a two liner, along the lines of what Optional Option was thinking:
Put sally and joe in the lookup
Enumerate the list of csv strings, splitting them and rejecting them if any element in the csv is in the lookup (implemented here as "accept them if all elements are not in the lookup")
Few unavoidable(unless you want to change your data storage) nasties though, I think. If you don't truly need a List out at the end and could work with an enumerable, remove the ToList to avoid an expensive copy operation
It could be a lot better by being a linked list of node that have string arrays instead of a list of strings that are csv- no splitting and efficient removal of nodes. Internally there will be a lot of list resizing and data shuffling with your current storage solution