Easiest way is probably to use a Dictionary:
Dictionary<IPAddress, int> ipInstances = new Dictionary<IPAddress, int>();
string[] lines = File.ReadAllLines(path);
foreach (string line in lines)
{
IPAddress ip = ...
if (!ipInstances.ContainsKey(ip))
{
ipInstances.Add(ip, 0);
}
ipInstances[ip]++;
}
You can then rank them using a simple Linq:
var ranked = ipInstances.OrderByDescending(kvp => kvp.Value).Select(kvp => kvp.Key);
[Suggestion -- Matt T Heffron]
If loading the whole file into memory at once is a concern, then a one line change should alleviate the issue:
Change:
string[] lines = File.ReadAllLines(path);
to be:
IEnumerable<string> lines = File.ReadLines(path);
Using
File.ReadLines()
instead of
File.ReadAllLines
lets the
foreach
loop deal with the lines one at a time...