I'm trying to loop through a given array and find how many duplicate values I have inside it. It works by going through a nested loop checking all elements of the array against each other and making sure it doesn't rise the counter if it's on the same index. But the problem is, it never counts up! Now it's either I don't understand the concept of valueOf vs indexOf or I'm just completely lost.
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.
GroupByLinq call or aDictionarywould be both faster and easier to implementIndexOfsearches will always be equal whenmyArr[i]andmyArr[j]are equal because it's starting it's search from the beginning. For you're example do you want the count to be 1 because there is one value that is duplicated, or 2 because two of the values in the list are duplicates?