string[] str={"1","1","2","5","2"};
统计的结果为 1,2个;2,2个;5,1个

解决方案 »

  1.   

    private System.Collections.Hashtable dt = new System.Collections.Hashtable();
    private System.Collections.ArrayList al = new System.Collections.ArrayList();
    public string TotalCount(string[] strs)
    {
    foreach(string str in strs)
    {
    bool isSearched = false;
    for(int i=0;i<al.Count ;i++)
    {
    if(al[i].ToString()==str)
    {
    int temp = (int) dt[str];
    dt[str] = temp+1;
    isSearched = true;
    break;
                     
    }
    }
    if(!isSearched)
    {
       dt.Add(str,1);
       al.Add(str);
    }

    }
    string returnStr = string.Empty;
    al.Sort();
    for(int i=0;i<al.Count ;i++)
    {
    returnStr+=al[i].ToString()+","+dt[al[i]]+"个;";
    }
    return returnStr; }
      

  2.   

    这是个最傻冒的方法,有没有人有更好的方法,比如不仅仅局限于string集合。
      

  3.   

    string[] str=new string[]{"1","1","2","5","2"};
    ArrayList a=new ArrayList();
    ArrayList b=new ArrayList();
    for(int i=0;i<str.Length;i++)
    {
    string c=str[i];
    if(!b.Contains(c))
    {
    int m=0;
    for(int j=0;j<str.Length;j++)
    {
    if(c==str[j])
    {
    m++;
    }
    }
    a.Add(m);
    b.Add(c);
    }
    }
    for(int m=0;m<a.Count;m++)
    {
    Response.Write(b[m]+","+a[m]+"个;");
    }
      

  4.   

    来一个只用一个集合类的,算法复杂度为 str.length-1,接龙!!!!!!
    string returnStr = string.Empty; 
    string[] str=new string[]{"1","1","2","5","2"};
    System.Collections.ArrayList al = new System.Collections.ArrayList();
    al.AddRange(strs);
    al.Sort();
    string tempStr = al[0].ToString();
    int index = 1;
    for(int i=1;i<al.Count;i++)
    {
      if(al[i].ToString()==tempStr)
     {
        ++index;
    continue;
    }
    else
    {
     returnStr += tempStr+","+index.ToString()+"个;";
    tempStr = al[i].ToString();
    index = 1;
    }
    }
    returnStr += tempStr+","+index.ToString()+"个;";