Re: [問題] 如何將陣列中重複的值刪掉,只列出不重 …

看板C_Sharp作者 (いまがその時だ)時間18年前 (2007/12/26 19:02), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《halaphone (Still Ballin)》之銘言: : 已經大概想出來要怎麼解了 : int[] data ={ 3, 1, 2, 3, 2, 1, 2 }; : int a, b = 0,sum; : Array.Sort(data);//{1,1,2,2,2,3,3} Dictionary<int, int> dic = new Dictionary<int, int>(); foreach (int i in data) { try{ dic.Add(i, 1); }catch(ArgumentException){ dic[i]++; } } foreach (KeyValuePair<int, int> d in dic) { Console.WriteLine("{0} => {1} times", d.Key, d.Value); } data = new int[dic.Keys.Count]; dic.Keys.CopyTo(data, 0); 用 Dictionary 試試? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.82.44 ※ 編輯: toki 來自: 122.116.82.44 (12/26 19:02)
文章代碼(AID): #17SZJ0-5 (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #17SZJ0-5 (C_Sharp)