您的位置:首页 > 其它

查找一个字符串中重复次数最多的字符

2010-07-19 12:15 483 查看
code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace AppTest
{
/// <summary>
///
/// </summary>
class Program
{
#region method one
//private static SortedDictionary<char, int> sd = new SortedDictionary<char, int>();
//static void Main(string[] args)
//{
//    string str = "1232143546534135164161";
//    foreach (char item in str)
//    {
//        Check(item);
//    }
//    char maxChar = Char.MinValue;
//    int maxValue = 0;
//    foreach (KeyValuePair<char,int> item in sd)
//    {
//        Console.WriteLine("Dictionary has a key :{0} and Value:{1}",item.Key,item.Value);
//        if (item.Value > maxValue)
//        {
//            maxValue = item.Value;
//            maxChar = item.Key;
//        }
//    }
//    Console.WriteLine("Max char is {0},times is {1}.",maxChar,maxValue);
//    Console.ReadLine();
//}
//private static void Check(char c)
//{
//    if (sd.ContainsKey(c))
//    {
//        sd[c]++;
//    }
//    else
//    {
//        sd.Add(c,1);
//    }
//}
#endregion

#region method two
//static void Main(string[] args)
//{
//    List<char> list = new List<char>();
//    string s = "1232143546534135164161 ";
//    list.AddRange(s.ToCharArray());
//    list.Sort();
//    char c = ' ';
//    int temp = 1;
//    char maxChar = ' ';
//    int maxCount = 0;

//    for (int i = 0; i < list.Count; i++)
//    {
//        if (i > 0)
//        {
//            if (list[i] == c)
//                temp++;
//            else
//            {
//                temp = 1;
//            }
//        }
//        if (temp > maxCount)
//        {
//            maxCount = temp;
//            maxChar = list[i];
//        }
//        c = list[i];
//    }

//    Console.WriteLine("字符 "+maxChar.ToString() + " 具有 " +maxCount.ToString());

//}
#endregion

#region method three
//static void Main(string[] args)
//{
//    string ss = "1232143546534135164161";
//    char[] c = ss.ToCharArray();
//    ArrayList al1 = new ArrayList();
//    ArrayList al2 = new ArrayList();
//    int i = 0;
//    int k = 0;
//    foreach (char item in c)
//    {
//        if (!al1.Contains(item))
//        {
//            al1.Add(item);
//            al2.Add(1);
//        }
//        else
//        {
//            al2.Insert(al1.IndexOf(item),Convert.ToInt32(al2[al1.IndexOf(item)].ToString()) + 1);
//            al2.RemoveAt(al1.IndexOf(item) + 1);
//        }
//    }

//    for (int j = 0; j < al2.Count; j++)
//    {
//       if(Convert.ToInt32(al2[j].ToString()) > i)
//       {
//           i = Convert.ToInt32(al2[j].ToString());
//           k = j;
//       }
//    }
//    string s = "字符 " + al1[k].ToString() + " 出现 " + i.ToString() + "次.";
//    Console.WriteLine(string.Format(" {0}", s));
//}
#endregion

#region method four
static void Main(string[] args)
{
string s = "1232143546534135164161";
StringBuilder sb = new StringBuilder(s);
List<int> charCount = new List<int>();
List<char> lchar = new List<char>();

int i,j;
i = 0; j = 1;

while (sb.Length > 0 && i < sb.Length)
{
charCount.Add(1);
lchar.Add(sb[0]);
while (sb.Length > 0 && j < sb.Length)
{
if (sb[0] == sb[j])
{
charCount[i]++;
}
j++;
}
sb.Replace(sb[0].ToString() ,"");
i++;
j = 1;
}

for (int k  = 0; k  < charCount.ToArray().Length; k ++)
{
Console.WriteLine(string.Format("  {0,4} {1}  "  , lchar[k].ToString(), charCount[k]));
}
Console.ReadLine();
}

#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐