我有一个包含如下数据的数组(字符串和时间数据用逗号分隔):
array[0]= 通道 1, 01:05:36
数组[1]= 通道 2, 02:25:36
数组[2]= 第 1 组,22:25:36
数组 [3]= 网络,41:40:09
数组[4]= LossOf,03:21:17
数组[5]= LossOf,01:13:28
数组[6]= 通道 1, 04:25:36
数组[7]= 通道 2, 00:25:36
. . . 数组[xxx]= xxx, xxx
我想计算所有重复项并确定每个重复项的平均时间,如下所示:
Item1,Channel 1,2次出现,每次出现的平均时间约为xx分钟
Item2,Channel 2,2次出现,每次出现的平均时间约为xx分钟
Item3,LossOf,2次出现,每次出现平均时间约xx分钟
时间格式为 hh:mm:ss
这是我到目前为止所做的,它只给了我重复的总次数:
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");
}
}