什么是获得列表中最高重复项目的最佳迭代算法? [英] what is the best iteration alogarith to get the highest repeated Item in list ?
本文介绍了什么是获得列表中最高重复项目的最佳迭代算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void AnalaysisFunc(List<CameraResult> camResult,bool IsFront)
{
CameraResult finalResult = new CameraResult();
for (int i = 0; i < camResult.Count; i++)
{
string[] key = { camResult[i].CarNoAr, camResult[i].CarNoEn, camResult[i].CitryCodeAr, camResult[i].CityCodeEn, camResult[i].CountryCode };
int[] repeatation = { 0, 0, 0, 0, 0 };
foreach (CameraResult cam in camResult)
{
if (cam.CarNoAr == key[0])
{
repeatation[0]++;
}
if (cam.CarNoEn == key[1])
{
repeatation[1]++;
}
if (cam.CitryCodeAr == key[2])
{
repeatation[2]++;
}
if (cam.CityCodeEn == key[3])
{
repeatation[3]++;
}
if (cam.CountryCode == key[4])
{
repeatation[4]++;
}
}
if (repeatation[0] >= camResult.Count / 2)
{
finalResult.CarNoAr = key[0];
}
if (repeatation[1] >= camResult.Count / 2)
{
finalResult.CarNoEn = key[1];
}
if (repeatation[2] >= camResult.Count / 2)
{
finalResult.CitryCodeAr = key[2];
}
if (repeatation[3] >= camResult.Count / 2)
{
finalResult.CityCodeEn = key[3];
}
if (repeatation[4] >= camResult.Count / 2)
{
finalResult.CountryCode = key[4];
}
}
//in case of cann't find repeatation take the last read in the list
if (camResult.Count > 0)
{
if (string.IsNullOrEmpty(finalResult.CarNoAr))
{
finalResult.CarNoAr = camResult[camResult.Count - 1].CarNoAr;
}
if (string.IsNullOrEmpty(finalResult.CarNoEn))
{
finalResult.CarNoEn = camResult[camResult.Count - 1].CarNoEn;
}
if (string.IsNullOrEmpty(finalResult.CityCodeEn))
{
finalResult.CityCodeEn = camResult[camResult.Count - 1].CityCodeEn;
}
if (string.IsNullOrEmpty(finalResult.CitryCodeAr))
{
finalResult.CitryCodeAr = camResult[camResult.Count - 1].CitryCodeAr;
}
if (string.IsNullOrEmpty(finalResult.CountryCode))
{
finalResult.CountryCode = camResult[camResult.Count - 1].CountryCode;
}
if (IsFront)
{
this.ForntCamThread = finalResult;
startAnalysisFront = false;
camResultFront.Clear();
}
else
{
this.BackCamThread = finalResult;
startAnalysisback = false;
camResultBack.Clear();
}
}
}
public class CameraResult
{
public CameraResult()
{ }
public CameraResult(string CountryCode, string CityCodeAr, string CityCodeEn,string carnoAr,string carNoEn)
{
this.CountryCode = CountryCode;
this.CityCodeEn = CityCodeEn;
this.CitryCodeAr = CityCodeAr;
this.CarNoAr = carnoAr;
this.CarNoEn = carNoEn;
}
string countryCode;
public string CountryCode
{
get { return countryCode; }
set { countryCode = value; }
}
string citryCodeAr;
public string CitryCodeAr
{
get { return citryCodeAr; }
set { citryCodeAr = value; }
}
string cityCodeEn;
public string CityCodeEn
{
get { return cityCodeEn; }
set { cityCodeEn = value; }
}
string carNoAr;
public string CarNoAr
{
get { return carNoAr; }
set { carNoAr = value; }
}
string carNoEn;
public string CarNoEn
{
get { return carNoEn; }
set { carNoEn = value; }
}
}
Is that a good way to achieve this goal with less iteration to get more performance in my case if I have list items 10 I My iteration no will be 100
推荐答案
使用LINQ计算时阅读以下内容:
http://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list -with-linq [ ^ ]
http://stackoverflow.com/questions/1 0600129 /逐个列表计数 [ ^ ]
Read the following on counting with LINQ :
http://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq[^]
http://stackoverflow.com/questions/10600129/group-by-and-count-in-list[^]
这篇关于什么是获得列表中最高重复项目的最佳迭代算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文