既然你问:“如果我们能得到哪些产品复制和启动索引和出现次数“,这是对特定需求的解决方案。
此输出以下:
2 was repeated 2 times starting at index 1
3 was repeated 3 times starting at index 4
4 was repeated 4 times starting at index 8
下面的代码:
using System;
using System.Collections.Generic;
namespace Demo
{
class DupeInfo
{
public string Text;
public int Index;
public int Count;
}
static class Program
{
static void Main()
{
var test = new[]
{
"1",
"2", "2",
"A",
"3", "3", "3",
"B",
"4", "4", "4", "4",
"C",
};
foreach (var dupeinfo in FindRepeats(test))
Console.WriteLine($"{dupeinfo.Text} was repeated {dupeinfo.Count} times starting at index {dupeinfo.Index}");
}
public static IEnumerable<DupeInfo> FindRepeats(IEnumerable<string> input)
{
int i = 0;
int j = 0;
int c = -1;
string prev = null;
foreach (var curr in input)
{
if (curr != prev)
{
if (c >= 0)
yield return new DupeInfo {Text = prev, Count = c + 2, Index = j};
c = -1;
j = i;
}
else
{
++c;
}
prev = curr;
++i;
}
if (c >= 0)
yield return new DupeInfo {Text = prev, Count = c + 2, Index = j};
}
}
}
你想要什么样的结果?真/假结果,重复部分的列表? –
显示您在试图解决此问题时编写的代码,以便我们可以查看出错的位置。不要忘记告诉我们为什么它不起作用。并调试你的代码。 –
@ LasseV.Karlsen,如果我们可以得到哪个项目是重复的,并开始索引和发生次数。 – Pankaj