查看一个int数组里边的每个数字出现过几次

public void aa()

{

  int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };

  Hashtable ht = new Hashtable();

  for (int i = 0; i < a.Length; i++)

  {

    if (ht.ContainsKey(a[i]))

    {

      ht.Add(a[i], (int)ht[i] + 1);

    }

    else

    {

      ht.Add(a[i], 1);

    }

  }

  string str = "";

  foreach (DictionaryEntry item in ht)

  {

    str += item.Key + ":" + item.Value + " ";

  }

}

  

你可能感兴趣的:(int)