I want to compare two lists and get the valid words into a new list.
var words = new List();
var badWords = new List();
//this i
Use Enumerable.Except:
List cleanList = words.Except(badWords).ToList();
This is efficient because Except
uses a set based approach.
An even more efficient approach is to avoid that "bad" words are added to the first list at all. For example by using a HashSet
var badWords = new HashSet(StringComparer.InvariantCultureIgnoreCase){ "Idiot", "Retarded", "Twat", "Fool", "Moron" };
string word = "idiot";
if (!badWords.Contains(word))
words.Add(word);