You can accomplish this very efficiently using regular expressions to match all vowels and replace them with empty strings:
var strArray = new List<string> { "cello", "guitar", "violin" };
var pattern = @"[aeiou]";
var noVowels = strArray.Select(item =>
Regex.Replace(item, pattern, "", RegexOptions.IgnoreCase));
foreach (var item in noVowels) {
Console.WriteLine(item);
}
This returns the outputs that you are looking for.
Your original attempt did not work because it evaluated each word separately for every unique vowel that it contained.
Update: I did some basic benchmarking of this solution versus Mathias' HashSet<char>
based solution (benchmark code here), including both Compile and Noncompiled versions of the Regex version. I ran it against an array of 2582 lorem-ipsum words, iterating 10 million times against the set (so going at ~25 billion words), running it in LinqPad, taking the average of 3 runs:
Init Each Time Init One Time
avg ms % diff avg ms % diff
Regex 586 +1% 586 -
Regex Compiled 581 - 593 +1%
HashSet 2550 +339% 641 +10%
It turns out that if you only initialize the HashSet
and pattern string
one time, then they have very similar performance. Regex
beats out Hashset
, but only barely (80 ms faster over 25 billion words) and Regex Compiled and Noncompiled perform almost identically. However, if you initialize the HashSet every single time you run it, then it kills performance for the HashSet approach.
The takeaway is that if you want to use the HashSet
approach, be sure to initialize your HashSet
only once per set of chars that you want to exclude.