0

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.
9
  • Do you have any limitiations imposed ? Because if you don't have any, a GroupByLinq call or a Dictionary would be both faster and easier to implement Commented Aug 11, 2017 at 13:14
  • Would you use LinQ with Group? Otherwise Dictionary<int, int> seems like an option as well Commented Aug 11, 2017 at 13:15
  • Your IndexOf searches will always be equal when myArr[i] and myArr[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? Commented Aug 11, 2017 at 13:16
  • 2
    Why not replace if (Array.IndexOf(myArr, myArr[i]) == Array.IndexOf(myArr, myArr[j])) with if(i==j) Commented Aug 11, 2017 at 13:16
  • 1
    @Harsh That would be better, but there's still issues with exactly what the OP wants to count and the fact that something like {1, 1, 1, 1) would give a count of 12 with your change. Commented Aug 11, 2017 at 13:19

3 Answers 3

7

Array.IndexOf searches for the first occurrence of the value in the array.

It looks like in your example you are trying to use it to make sure you're not comparing the same position in the array. In which case you could just replace that condition with if(i == j)

Sign up to request clarification or add additional context in comments.

2 Comments

That change would result in a count of 12 for a list like {1, 1, 1, 1}. I doubt that's the correct answer. There's more wrong than just the use of IndexOf here.
@juharr. Correct. But this answers the OP's question of why the counter never increments. The other problems are left as an exercise for the student.
3

Going along with the other answers/comments stating the Array.IndexOf is not the correct approach to this problem, and not exactly sure if you're allowed to use LINQ, but that is by far a better approach, especially using the GroupBy method.

I have created a dotnetfiddle for you to show what I am doing.

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.

As you can see, via the GroupBy method, you can find out how many numbers are repeating, as well as what the actual numbers are along with the number of occurrences that the actual number repeats itself in 1 line of code. Much cleaner, and efficient than using nesting for loops, but again I am not sure of what your limitations are.

I hope this helps.

Comments

1

You don't need to use the Array.IndexOf() function.

for example:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.