C# 统计列表中类似的相邻项<;字符串>;

C# 统计列表中类似的相邻项<;字符串>;,c#,list,count,adjacency-list,C#,List,Count,Adjacency List,我试图在列表中找到相似的相邻项并计算其数量,例如: List<string> list = new List<string> {"a", "a", "b", "d", "c", "c"}; 话虽如此,如果有比使用for循环迭代更容易在列表中找到类似相邻元素的方法,请给出建议。谢谢。您可以这样做: static IEnumerable<Tuple<string, int>> FindAdjacentItems(IEnumerable<stri

我试图在列表中找到相似的相邻项并计算其数量,例如:

List<string> list = new List<string> {"a", "a", "b", "d", "c", "c"};

话虽如此,如果有比使用for循环迭代更容易在列表中找到类似相邻元素的方法,请给出建议。谢谢。

您可以这样做:

static IEnumerable<Tuple<string, int>> FindAdjacentItems(IEnumerable<string> list)
{
    string previous = null;
    int count = 0;
    foreach (string item in list)
    {
        if (previous == item)
        {
            count++;
        }
        else
        {
            if (count > 1)
            {
                yield return Tuple.Create(previous, count);
            }
            count = 1;
        }
        previous = item;
    }

    if (count > 1)
    {
        yield return Tuple.Create(previous, count);
    }
}
静态IEnumerable FindAjacentItems(IEnumerable列表)
{
字符串previous=null;
整数计数=0;
foreach(列表中的字符串项)
{
如果(上一项==项目)
{
计数++;
}
其他的
{
如果(计数>1)
{
生成返回元组。创建(上一个,计数);
}
计数=1;
}
先前=项目;
}
如果(计数>1)
{
生成返回元组。创建(上一个,计数);
}
}
for(int i=0;i
检查以下内容:

Dictionary<char,int> dic=new Dictionary<char,int>();
for(int i=1;i<list.count;i++)
{
          if(list[i]==list[i-1])
                {
                      if(dic.ContainsKey(list[i]))
                                 {
                                   dic[list[i]]+=1;
                             }
                          else
                                 { 
                                    dic.Add(list[i],2)
                                  }
                  }
}
Dictionary dic=newdictionary();

for(int i=1;i要避免
ArgumentOutOfRangeException
请使用
for(int j=1;j
。用这种方法无法获得所需的答案。请尝试以下方法:

IEnumerable<Adjacent> CountAdjacents(List<string> source)
{
    var result = new List<Adjacent>();

    for (var i = 0; i < source.Count() - 1; i++)
    {
        if (source[i] == source[i + 1])
        {
            if (result.Any(x => x.Word == source[i]))
            {
                result.Single(x => x.Word == source[i]).Quantity++;
            }
            else
                result.Add(new Adjacent
                {
                    Word = source[i],
                    Quantity = 2
                });
        }
    }
    return result;
}

class Adjacent
{
    public string Word;
    public int Quantity;
}
IEnumerable countadjances(列表源)
{
var result=新列表();
对于(var i=0;ix.Word==source[i]))
{
结果.Single(x=>x.Word==source[i]).Quantity++;
}
其他的
结果。添加(新的相邻项)
{
字=来源[i],
数量=2
});
}
}
返回结果;
}
类邻接
{
公共字符串;
公共整数;
}

维护一个256大小的整数数组,初始化为1。对i=0到i-2运行一个循环[O(n)],将每个字符与下一个字符进行比较。如果相同,则查找字符的ascii值,并在数组中增加相应的值。
希望这有帮助!

请定义“相似”;你实际上是指“相等”吗?那么
{“a”、“a”、“b”、“a”、“a”、“a”}
的期望输出是什么?你的代码示例与你在示例列表中描述的期望不匹配。这也会找到非相邻的项,而且效率非常低(
O(n²)
当它可能是
O(n)时
Dictionary<char,int> dic=new Dictionary<char,int>();
for(int i=1;i<list.count;i++)
{
          if(list[i]==list[i-1])
                {
                      if(dic.ContainsKey(list[i]))
                                 {
                                   dic[list[i]]+=1;
                             }
                          else
                                 { 
                                    dic.Add(list[i],2)
                                  }
                  }
}
IEnumerable<Adjacent> CountAdjacents(List<string> source)
{
    var result = new List<Adjacent>();

    for (var i = 0; i < source.Count() - 1; i++)
    {
        if (source[i] == source[i + 1])
        {
            if (result.Any(x => x.Word == source[i]))
            {
                result.Single(x => x.Word == source[i]).Quantity++;
            }
            else
                result.Add(new Adjacent
                {
                    Word = source[i],
                    Quantity = 2
                });
        }
    }
    return result;
}

class Adjacent
{
    public string Word;
    public int Quantity;
}