[英]Find a string list whether containing same element more than once
我正在为产品销售网站编写自己的特定Web搜寻器。 由于它们的编码性质很差,我得到的URL指向同一页面。
例子一
http://www.hizlial.com/bilgisayar/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
例如,上面的页面与下面的页面相同
http://www.hizlial.com/bilgisayar-bilesenleri/bilgisayar/yazicilar/samsung-scx-3200-tarayici-fotokopi-lazer-yazici_30.033.1271.0043.htm
如您所见,当通过'/'
字符分割时,它包含2个“ bilgisayar”元素
所以我想要的是我想像这样分割网址
string[] lstSPlit = srURL.Split('/');
之后,检查该列表是否包含同一元素多次。 任何元素。 如果包含任何元素,我将跳过该网址,因为我已经从其他页面提取了真实网址。 那么最好的方法是什么?
更长的版本
string[] lstSPlit = srHref.Split('/');
bool blDoNotAdd = false;
HashSet<string> splitHashSet=new HashSet<string>();
foreach (var vrLstValue in lstSPlit)
{
if (vrLstValue.Length > 1)
{
if (splitHashSet.Contains(vrLstValue) == false)
{
splitHashSet.Add(vrLstValue);
}
else
{
blDoNotAdd = true;
break;
}
}
}
if (list.Distinct().Count() < list.Count)
这应该比分组更快。 (我没有测量)
通过编写自己的扩展方法(将项目添加到HashSet<T>
并在Add()
返回false时立即返回false Add()
可以使其速度更快。
您甚至可以使用邪恶的速记来做到这一点:
if (!list.All(new HashSet<string>().Add))
if(lstSPlit.GroupBy(i => i).Where(g => g.Count() > 1).Any())
{
// found more than once
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.