如何找到没有数组列表中的重复值 [英] how to find no of duplicate values in arraylist

查看:151
本文介绍了如何找到没有数组列表中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也有价值观其中有些是重复的ArrayList。我需要重复值的计数。这是可能在C#


解决方案

 公众解释< T,INT> CountOccurences< T>(IEnumerable的< T>项目){
变种OCCURENCES =新词典< T,INT>();
的foreach(在项目牛逼项){
如果(occurences.ContainsKey(项目)){
OCCURENCES [项目] ++;
}其他{
occurences.Add(项目,1);
}
}
返回OCCURENCES;
}


I have a arraylist which has values some of them are repeated. I need the count of the repeated values. Is this possible in c#?

解决方案

public Dictionary<T,int> CountOccurences<T>(IEnumerable<T> items) {
  var occurences = new Dictionary<T,int>();
  foreach(T item in items) {
    if(occurences.ContainsKey(item)) {
      occurences[item]++;
    } else {
      occurences.Add(item, 1);
    }
  }
  return occurences;
}

这篇关于如何找到没有数组列表中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆