熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> 數據結構 >> 正文

如何搜索一維數組中重復元素的個數?

2022-06-13   來源: 數據結構 

<方法一>int[] arr ={ };
Dictionary<int int> dic = new Dictionary<int int>();
foreach (int i in arr)
{
if (dicContainsKey(i))
dic = dic + ;
else
dicAdd(i );
}
//查看結果
foreach (int j in dicKeys)
{
richTextBoxText += jToString() + &#;的個數有 &#; + dic[j] + &#;個\n&#;;
}
<方法二>int[] arr = { };
var query =
from a in arr
group arr by a into g
orderby gCount()
select new
{
gKey
count = gCount()
};
foreach (var i in query)
{
ConsoleWriteLine(&#;{}出現{}次&#; iKey icount);
}


From:http://tw.wingwit.com/Article/program/sjjg/201405/30734.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.