[英]Output how many duplicates in an array
I'm looking to output the number of duplicates for each int in an array eg the array 1,2,3,4,1,1,3 would output 1:3, 2:1, 3:2, 4:1.我正在寻找 output 数组中每个 int 的重复项数,例如数组 1,2,3,4,1,1,3 将是 output 1:3、2:1、3:2、4:1。 at the moment no matter how many of each number there is the dictionary only counts one and outputs every value to be 1.目前无论每个数字有多少,字典都只计算一个并将每个值输出为 1。
static void Main(string[] args)
{
Console.WriteLine("Type 10 numbers");
int[] arr1 = new int[10];
for (int i = 0; i < arr1.Length; i++)
{
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
output(arr1);
Console.ReadKey();
}
static void output(int[] arr1)
{
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i =0; i < arr1.Length; i++)
{
if (dict.ContainsKey(arr1[i]))
{
int c = dict[arr1[i]];
dict[arr1[i]] = c++;
}
else
{
dict.Add(arr1[i], 1);
}
}
for (int i =0; i<arr1.Length; i++)
{
Console.WriteLine(arr1[i] + ":" + dict[arr1[i]]);
}
}
I assume you want to write the algorithm to group the numbers by yourself.我假设您想自己编写算法来对数字进行分组。 If not, have a look at LINQ, which provides already a lot of collection operations which makes life a lot more easier.如果没有,请查看 LINQ,它已经提供了很多集合操作,这使生活变得更加轻松。 In your case a GroupBy should already do the trick.在您的情况下, GroupBy 应该已经可以解决问题。
The problem of your implementation lies in the following line:您的实现问题在于以下行:
dict[arr1[i]] = c++;
The reason is you are incrementint c
after setting it as new dictionary value.原因是您将其设置为新的字典值后 incrementint c
。 c++
and ++c
are different operations, this is described in What is the difference between ++i and i++? c++
和++c
是不同的操作,这在 ++i 和 i++ 有什么区别? . . To solve your problem and increment before setting teh value use解决您的问题并在设置值之前增加使用
dict[arr1[i]] = ++c;
Note that you also could write this step of incrementation more compact like请注意,您也可以编写更紧凑的增量步骤,例如
dict[arr1[i]]++;
If you want to use Linq如果你想使用 Linq
var array = new int[] { 1, 2, 3, 4, 1, 1, 3 };
var str= string.Join(",", array.GroupBy(x => x).Select(g => g.Key + ":" + g.Count()));
Console.WriteLine(str);
While Fruchtzwerg is correct, you're returning c
before it's incremented, you shouldn't even be making a temp variable.虽然 Fruchtzwerg 是正确的,但您在增加之前返回c
,您甚至不应该创建临时变量。
Just do:做就是了:
dict[arr1[i]] += 1; //or dict[arr1[i]]++;
You also shouldn't use that for-loop to loop through the dictionary values.您也不应该使用该 for 循环来遍历字典值。 As it will not print out the way you probably want it to.因为它不会按照您可能想要的方式打印出来。
foreach(var kvp in dict)
{
Console.WriteLine(kvp.Key + ":" + kvp.Value);
}
Try using the above foreach loop尝试使用上面的 foreach 循环
You can try this:你可以试试这个:
class Program{
static void Main(string[] args){
int[] array = new int[10];
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Type in the number for index: " + i);
array[i] = Convert.ToInt32(Console.ReadLine());
Duplicate(array);
}
}
public static void Duplicate(int[] array)
{
List<int> done = new List<int>();
for (int i = 0; i < array.Length; i++)
{
if (check(array[i], done) == false)
{
Console.WriteLine();
Console.WriteLine(array[i] + ":" + dupe(array[i], array));
}
}
}
public static int dupe(int number, int[] array)
{
int duplicate = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == number)
{
duplicate++;
}
}
return duplicate;
}
public static bool check(int number, List<int> list)
{
bool b = false;
for (int i = 0; i < list.Count; )
{
if (number == list[i])
{
b = true;
i++;
}
else
{
i++;
}
}
return b;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.