Dictionary重复值并提示

无二法 2016-09-24 09:43:16
Dictionary<int, int> abc

abc.add[1,3]
abc.add[2,2]
abc.add[3,3]
abc.add[4,1]
abc.add[5,3]
abc.add[6,4]

要求:判断 value中重复的,输出;重复项为 1,3 3,1 5,3 ,,,mesbox.sow 输出key和value两项 .。其中value不重复的不用输出。 谢谢
...全文
419 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
oolongTea 2016-09-24
  • 打赏
  • 举报
回复
曲线救国

Dictionary<int, int> abc = new Dictionary<int, int>();

            abc.Add(1, 3);
            abc.Add(2, 2);
            abc.Add(3, 3);
            abc.Add(4, 1);
            abc.Add(5, 3);
            abc.Add(6, 4);

            System.Collections.Hashtable hst1 = new System.Collections.Hashtable();
            System.Collections.Hashtable hst2 = new System.Collections.Hashtable();

            foreach (var item in abc)
            {
                if (!hst1.ContainsValue(item.Value))
                {
                    hst1.Add(item.Key, item.Value);
                }
                else
                {
                    if (!hst2.ContainsValue(item.Value))
                        hst2.Add(item.Key, item.Value);
                }
            }

            
            foreach (var item in abc)
            {
                foreach (System.Collections.DictionaryEntry de in hst2)
                {
                    if (item.Value == (int)de.Value)
                    {
                        System.Windows.Forms.MessageBox.Show(item.Key.ToString() + "    " + item.Value.ToString());
                    }
                }
            }
无二法 2016-09-24
  • 打赏
  • 举报
回复
十分满意,谢谢
巴士上的邂逅 2016-09-24
  • 打赏
  • 举报
回复
 Dictionary<int, int> abc = new Dictionary<int, int>();
 abc.Add(1, 3);
 abc.Add(2, 2);
 abc.Add(3, 3);
 abc.Add(4, 1);
 abc.Add(5, 3);
 abc.Add(6, 4);
 var result = abc.GroupBy(a => a.Value).Where(g => g.Count(kv => kv.Value == g.Key) > 1).SelectMany(g => g);
 foreach (var kv in result)
 {
     Console.WriteLine(string.Format("[{0},{1}]", kv.Key, kv.Value));
 }
结果
[1,3]
[3,3]
[5,3]

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧