Created
April 1, 2015 14:57
-
-
Save dlidstrom/56509a4014ac6a7b9910 to your computer and use it in GitHub Desktop.
Output number of words occurring exactly n times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
static void Main() | |
{ | |
var words = File.ReadAllText(@"C:\Programming\words.txt") | |
.Split(' ', '\r', '\n') | |
.Select(x => x.Trim()) | |
.Where(x => !string.IsNullOrEmpty(x)) | |
.ToArray(); | |
var candidates = new Dictionary<string, int>(); | |
var picked = new HashSet<string>(); | |
var count = 9; | |
foreach (var word in words) | |
{ | |
int currentSeenCount; | |
if (candidates.TryGetValue(word, out currentSeenCount) == false) | |
{ | |
candidates.Add(word, 0); | |
} | |
candidates[word] = ++currentSeenCount; | |
if (currentSeenCount == count) | |
{ | |
picked.Add(word); | |
} | |
else if (currentSeenCount > count) | |
{ | |
picked.Remove(word); | |
} | |
} | |
Console.WriteLine("Found these words occurring {0} times:", count); | |
foreach (var x in picked) | |
{ | |
Console.WriteLine(x); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment