C#-特定的int值在数组中出现多少次?无Linq技术
已经看到了很多答案,但找不到合适的答案 我需要一个高效的算法或者C#中的方法来计算特定int值在数组中出现的次数不带Linq。数组大小>=100,每个元素不大于100 有此代码:C#-特定的int值在数组中出现多少次?无Linq技术,c#,algorithm,search,C#,Algorithm,Search,已经看到了很多答案,但找不到合适的答案 我需要一个高效的算法或者C#中的方法来计算特定int值在数组中出现的次数不带Linq。数组大小>=100,每个元素不大于100 有此代码: for (int i = 0; i < 100; i++) // get each number for counting { counter = 0; // zero counter for next number comparison
for (int i = 0; i < 100; i++) // get each number for counting
{
counter = 0; // zero counter for next number comparison
for (int a = 0; a < array.Length; i++)
{
if (i == array[a])
{
counter++;
if (max < counter) max = counter; // save max-appeared num
}
}
}
for(int i=0;i<100;i++)//获取每个数字进行计数
{
计数器=0;//下一个数字比较的零计数器
for(int a=0;a
它在测试质询中将我显示为结果消息“由于超时而终止”。我想这段代码需要很多时间才能解决。有其他选择吗?程序代码:
int[] num = { 1, 1, 1, 3, 3, 4, 5, 6, 7, 0 };
int[] count = new int[10];
//Loop through 0-9 and count the occurances
for (int x = 0; x < 10; x++)
for (int y = 0; y < num.Length; y++)
if (num[y] == x)
count[x]++;
//For displaying output only
for (int x = 0; x < 10; x++)
Console.WriteLine("Number " + x + " appears " + count[x] + " times");
我理解当你所有的同学都完成了他们的作业,而你还在挣扎时的感觉有多糟糕。我的代码应该足够简单,便于您学习 程序代码:
int[] num = { 1, 1, 1, 3, 3, 4, 5, 6, 7, 0 };
int[] count = new int[10];
//Loop through 0-9 and count the occurances
for (int x = 0; x < 10; x++)
for (int y = 0; y < num.Length; y++)
if (num[y] == x)
count[x]++;
//For displaying output only
for (int x = 0; x < 10; x++)
Console.WriteLine("Number " + x + " appears " + count[x] + " times");
我理解当你所有的同学都完成了他们的作业,而你还在挣扎时的感觉有多糟糕。我的代码应该足够简单,便于您学习 您可以利用
每个元素不大于100
并将所有频率声明为一个数组(只能是101
项:[0..100]
):
输出:
for (int i = 0; i < freqs.Length; ++i)
Console.WriteLine("Number {0} appears {1} times", i, freqs[i]);
你可以利用
每个元素不大于100
并将所有频率声明为一个数组(只能是101
项:[0..100]
):
输出:
for (int i = 0; i < freqs.Length; ++i)
Console.WriteLine("Number {0} appears {1} times", i, freqs[i]);
可以避免执行两个循环
// Prepare the test array (this is done before entering the counting loop)
Random rnd = new Random();
int[] array = new int[100]; // 100 or whatever....
for (int i = 0; i < array.Length; i++)
array[i] = rnd.Next(1, 101); // 1-100 range
// The max occurences of a number
int maxOccurence = 0;
// The number with the max occurences
int currentMax = 0;
// One bigger to accomodate the 100....
int[] occurrences = new int[array.Length + 1];
// Loop and check on the test array
for (int a = 0; a < array.Length; a++)
{
// The current number to examine
int number = array[a];
// Increment the occurences counter for the number
occurrences[number]++;
// Check if we have a new max....
if (occurrences[number] > maxOccurence)
{
// Save the new leader, both the new max occurence and number
maxOccurence = occurrences[number];
currentMax = number;
}
}
Console.WriteLine(maxOccurence);
Console.WriteLine(currentMax);
//准备测试数组(在进入计数循环之前完成)
随机rnd=新随机();
int[]数组=新的int[100];//100或其他什么。。。。
for(int i=0;i最大发生次数)
{
//保存新的引线,包括新的最大发生次数和编号
MaxOccurrence=出现次数[数量];
currentMax=数量;
}
}
控制台写入线(MaxOccurrence);
控制台写入线(currentMax);
但是,这仍然无法检测两个或多个数字是否具有相同的发生计数。您可以避免执行两个循环
// Prepare the test array (this is done before entering the counting loop)
Random rnd = new Random();
int[] array = new int[100]; // 100 or whatever....
for (int i = 0; i < array.Length; i++)
array[i] = rnd.Next(1, 101); // 1-100 range
// The max occurences of a number
int maxOccurence = 0;
// The number with the max occurences
int currentMax = 0;
// One bigger to accomodate the 100....
int[] occurrences = new int[array.Length + 1];
// Loop and check on the test array
for (int a = 0; a < array.Length; a++)
{
// The current number to examine
int number = array[a];
// Increment the occurences counter for the number
occurrences[number]++;
// Check if we have a new max....
if (occurrences[number] > maxOccurence)
{
// Save the new leader, both the new max occurence and number
maxOccurence = occurrences[number];
currentMax = number;
}
}
Console.WriteLine(maxOccurence);
Console.WriteLine(currentMax);
//准备测试数组(在进入计数循环之前完成)
随机rnd=新随机();
int[]数组=新的int[100];//100或其他什么。。。。
for(int i=0;i最大发生次数)
{
//保存新的引线,包括新的最大发生次数和编号
MaxOccurrence=出现次数[数量];
currentMax=数量;
}
}
控制台写入线(MaxOccurrence);
控制台写入线(currentMax);
但是,这仍然无法检测两个或多个数字是否具有相同的发生计数。您可以执行以下操作:
var counters = new int[101]; // 0 to 100
var max = 0;
var maxCount = 0;
foreach (var n in input)
{
counters[n] = counters[n] + 1;
if (counters[n] > maxCount)
{
maxCount = counters[n];
max = n;
}
}
return max;
您可以执行以下操作:
var counters = new int[101]; // 0 to 100
var max = 0;
var maxCount = 0;
foreach (var n in input)
{
counters[n] = counters[n] + 1;
if (counters[n] > maxCount)
{
maxCount = counters[n];
max = n;
}
}
return max;
代码似乎与您的描述不一致。您是否需要1)计算一个特定值出现的次数(那么为什么是
max
变量和外部循环)?2) 计算每个唯一值出现的次数(如果不是,为什么是外部循环)?3) 查找数组中出现次数最多的值(max变量)?4) 发生次数最多的数字发生了多少次(但不是哪个数字)?你是对的。我应该更具体一点。是的,我说的是3:)如果数组中的数字不大于100(我也假设为0或更高),那么只需创建一个包含101个插槽的数组(如果包含0),然后为数组中的每个数字增加该数组中相应的插槽。在您浏览了一次数组之后,您可以浏览这个101元素的数组,并找出哪个元素出现得最多。最有可能的原因是10倍(非常大的数组)的好提示。我试试看。TNX当你说没有linq时,你的意思是也没有泛型吗?因为这两件事不一样,代码似乎与您的描述不一致。您是否需要1)计算一个特定值出现的次数(那么为什么是max
变量和外部循环)?2) 计算每个唯一值出现的次数(如果不是,为什么是外部循环)?3) 查找数组中出现次数最多的值(max变量)?4) 发生次数最多的数字发生了多少次(但不是哪个数字)?你是对的。我应该更具体一点。是的,我说的是3:)如果数组中的数字不大于100(我也假设为0或更高),那么只需创建一个包含101个插槽的数组(如果包含0),然后为数组中的每个数字增加该数组中相应的插槽。在您浏览了一次数组之后,您可以浏览这个101元素的数组,并找出哪个元素出现得最多。很可能