C# 从输入数据流中查找重复计数。。。
我试了好几次,但都找不到解决问题的办法。这里是我的txt文件,如下所示C# 从输入数据流中查找重复计数。。。,c#,C#,我试了好几次,但都找不到解决问题的办法。这里是我的txt文件,如下所示 695 748 555 695 748 852 639 748 我将for循环用于读取数据,并将它们放入数组中。现在我想从输入的txt数据中过滤重复的数字。如何计算重复数据的数量 static void Main(string[] args) { int x = 0; int count = 0; String[] line = File.ReadAllLin
695
748
555
695
748
852
639
748
我将for循环用于读取数据,并将它们放入数组中。现在我想从输入的txt数据中过滤重复的数字。如何计算重复数据的数量
static void Main(string[] args)
{
int x = 0;
int count = 0;
String[] line = File.ReadAllLines("C:/num.txt");
int n = line.Length;
String[] res = new String[n];
for (int i = 0; i < n; i++)
{
res[i] = line[i].Substring(x,x+8);
Console.WriteLine(res[i]);
}
Console.ReadLine();
}
不知何故,你必须记录你以前见过的东西 一种方法是在你第一次看到这些数字时就把它们放在一个列表中。如果列表中已经有给定的数字,请在后面出现时将其过滤掉 下面是一个列表示例。请注意,获取输入子字符串的代码崩溃
static void Main(string[] args)
{
int x = 0;
int count = 0;
String[] line = new string[] { "123", "456", "123" }; //File.ReadAllLines("C:/num.txt");
int n = line.Length;
String[] res = new String[n];
List<string> observedValues = new List<string>();
for (int i = 0; i < n; i++)
{
string consider = line[i]; // This code crashes: .Substring(x, x + 8);
if (!observedValues.Contains(consider))
{
observedValues.Add(consider);
res[i] = consider;
Console.WriteLine(res[i]);
}
else
{
Console.WriteLine("Skipped value: " + consider + " on line " + i);
}
Console.ReadLine();
}
}
另一种方法是对输入进行预排序,以便重复项相邻
例如:
注意,您可能希望在排序之前删除输入中的空白。前导空格将破坏此代码
static void Main(string[] args)
{
int x = 0;
int count = 0;
String[] line = new string[] { "123", "456", "123" }; //File.ReadAllLines("C:/num.txt");
int n = line.Length;
String[] res = new String[n];
string previous = null;
Array.Sort(line); // Ensures that equal values are adjacent
for (int i = 0; i < n; i++)
{
string consider = line[i].Trim(); // Note leading whitespace will break this.
if (consider != previous)
{
previous = consider;
res[i] = consider;
Console.WriteLine(res[i]);
}
else
{
Console.WriteLine("Skipped value: " + consider + " on line " + i);
}
Console.ReadLine();
}
}
不知何故,你必须记录你以前见过的东西 一种方法是在你第一次看到这些数字时就把它们放在一个列表中。如果列表中已经有给定的数字,请在后面出现时将其过滤掉 下面是一个列表示例。请注意,获取输入子字符串的代码崩溃
static void Main(string[] args)
{
int x = 0;
int count = 0;
String[] line = new string[] { "123", "456", "123" }; //File.ReadAllLines("C:/num.txt");
int n = line.Length;
String[] res = new String[n];
List<string> observedValues = new List<string>();
for (int i = 0; i < n; i++)
{
string consider = line[i]; // This code crashes: .Substring(x, x + 8);
if (!observedValues.Contains(consider))
{
observedValues.Add(consider);
res[i] = consider;
Console.WriteLine(res[i]);
}
else
{
Console.WriteLine("Skipped value: " + consider + " on line " + i);
}
Console.ReadLine();
}
}
另一种方法是对输入进行预排序,以便重复项相邻
例如:
注意,您可能希望在排序之前删除输入中的空白。前导空格将破坏此代码
static void Main(string[] args)
{
int x = 0;
int count = 0;
String[] line = new string[] { "123", "456", "123" }; //File.ReadAllLines("C:/num.txt");
int n = line.Length;
String[] res = new String[n];
string previous = null;
Array.Sort(line); // Ensures that equal values are adjacent
for (int i = 0; i < n; i++)
{
string consider = line[i].Trim(); // Note leading whitespace will break this.
if (consider != previous)
{
previous = consider;
res[i] = consider;
Console.WriteLine(res[i]);
}
else
{
Console.WriteLine("Skipped value: " + consider + " on line " + i);
}
Console.ReadLine();
}
}
你使用GroupBy
你使用GroupBy
现在我想从输入的txt数据中过滤重复的数字
如果您只需要过滤掉重复项,则可以使用以下方法:
String[] line = File.ReadAllLines("C:/num.txt");
var filteredLines = line.Distinct();
foreach (var item in filteredLines)
Console.WriteLine(item);
现在我想从输入的txt数据中过滤重复的数字
如果您只需要过滤掉重复项,则可以使用以下方法:
String[] line = File.ReadAllLines("C:/num.txt");
var filteredLines = line.Distinct();
foreach (var item in filteredLines)
Console.WriteLine(item);
@查马尔:发布的示例尽可能接近您现有的代码。@查马尔:您的代码实际上并没有增加“count”变量。如果您输出一个新的数字,您可能希望这样做。@Chamal:发布的示例尽可能接近您的现有代码。@Chamal:您的代码实际上并没有增加“count”变量。在输出新数字的情况下,您可能希望这样做。