I have an array that contains data as follows (string and time data separated by a comma):
array[0]= Channel 1, 01:05:36
array[1]= Channel 2, 02:25:36
array[2]= Group 1, 22:25:36
array[3]= Netwk, 41:40:09
array[4]= LossOf, 03:21:17
array[5]= LossOf, 01:13:28
array[6]= Channel 1, 04:25:36
array[7]= Channel 2, 00:25:36
.
.
.
array[xxx]= xxx, xxx
I would like to count all duplicates items and determine average time for each duplicate found as follows:
Item1, Channel 1, 2 occurrences, average time for each occurrence is about xx minutes
Item2, Channel 2, 2 occurrences, average time for each occurrence is about xx minutes
Item3, LossOf, 2 occurrences, average time for each occurrence is about xx minutes
The time format is hh:mm:ss
This is what I have done so far, which only gives me total times of duplicates:
public void CountDuplicates(string[] myStringArray)
{
//count duplicates
ArrayList list = new ArrayList();
int loopCnt=0;
foreach (string item in myStringArray)
{
if (!String.IsNullOrEmpty(myStringArray[loopCnt]) == true)
list.Add(item);
loopCnt++;
}
loopCnt = 0;
Dictionary<string, int> distinctItems = new Dictionary<string, int>();
foreach (string item in list)
{
if (!distinctItems.ContainsKey(item))
{
distinctItems.Add(item, 0);
loopCnt++;
}
distinctItems[item] += 1;
}
foreach (KeyValuePair<string, int> distinctItem in distinctItems)
{
txtDisplayResults.AppendText("Alarm Error: " + distinctItem.Key + ", How many times: " + distinctItem.Value + "\r\n");
}
}
Maybe this:
a channel class to hold your values
your sample data
the query
output:
demo: http://ideone.com/6dF6s