Array.IndexOf 与给定索引问题的值
Array.IndexOf vs. value of given index issue
我正在尝试遍历给定数组并查找其中有多少重复值。它的工作原理是通过一个嵌套循环检查数组的所有元素,并确保它在同一索引上时不会增加计数器。但问题是,它从来都算不上!
现在要么我不理解 valueOf 与 indexOf 的概念,要么我完全迷路了。
int[] myArr = new int[] { 10, 5, 5 };
int counter = 0;
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr.Length; j++)
{
if (Array.IndexOf(myArr, myArr[i]) == Array.IndexOf(myArr, myArr[j]))
{
continue;
}
else if (myArr[i] == myArr[j])
{
counter++;
}
}
}
Console.WriteLine("There are {0} repeating values in the array.", counter);
// Output: There are 0 repeating values in the array.
Array.IndexOf 搜索数组中第一次出现的值。
在您的示例中,您似乎在尝试使用它来确保您没有比较数组中的相同位置。在这种情况下,您可以将该条件替换为 if(i == j)
您不需要使用 Array.IndexOf()
功能。
例如:
int[] myArr = new int[] { 10, 5, 5, 5};
int counter = 0;
List<int> dups = new List<int>();
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr.Length; j++)
{
if (i != j && myArr[i] == myArr[j] && !dups.Contains(i))
{
dups.Add(j);
counter++;
}
}
}
Console.WriteLine("There are {0} repeating values in the array.", counter);
// Output: There are 2 repeating values in the array.
与其他 answers/comments 一起说明 Array.IndexOf
不是解决此问题的正确方法,并且不确定是否允许您使用 LINQ,但到目前为止这是一个更好的方法,尤其是使用 GroupBy
方法。
我创建了一个 dotnetfiddle 给你展示我在做什么。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] myArr = new int[] { 10, 5, 5, 3, 3, 3 };
// int counter = 0; - now this is no longer needed
var numbersThatAreDuplicates = myArr.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => new { number = x.Key, countOfNumber = x.Count()}).ToList();
Console.WriteLine("There are {0} repeating values in the array.", numbersThatAreDuplicates.Count);
foreach (var item in numbersThatAreDuplicates)
{
Console.WriteLine(item.number + " repeats itself " + item.countOfNumber + " times.");
}
}
}
// Output
// There are 2 repeating values in the array.
// 5 repeats itself 2 times.
// 3 repeats itself 3 times.
如您所见,通过 GroupBy
方法,您可以找出有多少数字重复,以及实际数字是多少,以及实际数字重复出现的次数1行代码。比使用嵌套 for 循环更干净、更高效,但我还是不确定你的限制是什么。
希望对您有所帮助。
我正在尝试遍历给定数组并查找其中有多少重复值。它的工作原理是通过一个嵌套循环检查数组的所有元素,并确保它在同一索引上时不会增加计数器。但问题是,它从来都算不上! 现在要么我不理解 valueOf 与 indexOf 的概念,要么我完全迷路了。
int[] myArr = new int[] { 10, 5, 5 };
int counter = 0;
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr.Length; j++)
{
if (Array.IndexOf(myArr, myArr[i]) == Array.IndexOf(myArr, myArr[j]))
{
continue;
}
else if (myArr[i] == myArr[j])
{
counter++;
}
}
}
Console.WriteLine("There are {0} repeating values in the array.", counter);
// Output: There are 0 repeating values in the array.
Array.IndexOf 搜索数组中第一次出现的值。
在您的示例中,您似乎在尝试使用它来确保您没有比较数组中的相同位置。在这种情况下,您可以将该条件替换为 if(i == j)
您不需要使用 Array.IndexOf()
功能。
例如:
int[] myArr = new int[] { 10, 5, 5, 5};
int counter = 0;
List<int> dups = new List<int>();
for (int i = 0; i < myArr.Length; i++)
{
for (int j = 0; j < myArr.Length; j++)
{
if (i != j && myArr[i] == myArr[j] && !dups.Contains(i))
{
dups.Add(j);
counter++;
}
}
}
Console.WriteLine("There are {0} repeating values in the array.", counter);
// Output: There are 2 repeating values in the array.
与其他 answers/comments 一起说明 Array.IndexOf
不是解决此问题的正确方法,并且不确定是否允许您使用 LINQ,但到目前为止这是一个更好的方法,尤其是使用 GroupBy
方法。
我创建了一个 dotnetfiddle 给你展示我在做什么。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int[] myArr = new int[] { 10, 5, 5, 3, 3, 3 };
// int counter = 0; - now this is no longer needed
var numbersThatAreDuplicates = myArr.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => new { number = x.Key, countOfNumber = x.Count()}).ToList();
Console.WriteLine("There are {0} repeating values in the array.", numbersThatAreDuplicates.Count);
foreach (var item in numbersThatAreDuplicates)
{
Console.WriteLine(item.number + " repeats itself " + item.countOfNumber + " times.");
}
}
}
// Output
// There are 2 repeating values in the array.
// 5 repeats itself 2 times.
// 3 repeats itself 3 times.
如您所见,通过 GroupBy
方法,您可以找出有多少数字重复,以及实际数字是多少,以及实际数字重复出现的次数1行代码。比使用嵌套 for 循环更干净、更高效,但我还是不确定你的限制是什么。
希望对您有所帮助。