Finding duplicate integers in an array and display

2019-01-17 15:55发布

I'm working on a code that prints out duplicated integers from an array with the number of their occurrence. I'm not allowed to use LINQ, just a simple code. I think I'm so close but confused about how to get a correct output:

class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length - 1 ; j++)
            {

               if(array[j] == array[j+1])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}

17条回答
forever°为你锁心
2楼-- · 2019-01-17 16:22

You can check the following codes. The codes are working.

 class Program {
    static void Main(string[] args) {
      int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
      int count = 1;
      for (int i = 0; i < array.Length; i++) {
        for (int j = i + 1; j < array.Length; j++) {

          if (array[j] == array[j])
            count = count + 1;
        }
        Console.WriteLine("\t\n " + array[i] + " out of " + count);
        Console.ReadKey();
      }
    }
  }
查看更多
爷的心禁止访问
3楼-- · 2019-01-17 16:28

You made a minor mistake of using J instead of i ...

class Program
{
    static void Main(string[] args)
    {              
        int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
        int count = 1;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i; j < array.Length - 1 ; j++)
            {
               if(array[i] == array[j+1])
                  count = count + 1;
            }
            Console.WriteLine("\t\n " + array[i] + "occurse" + count);
            Console.ReadKey();
        }
    }
}
查看更多
地球回转人心会变
4楼-- · 2019-01-17 16:28

Since you can't use LINQ, you can do this with collections and loops instead:

static void Main(string[] args)
{              
    int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
    var dict = new Dictionary<int, int>();

    foreach(var value in array)
    {
        if (dict.ContainsKey(value))
            dict[value]++;
        else
            dict[value] = 1;
    }

    foreach(var pair in dict)
        Console.WriteLine("Value {0} occurred {1} times.", pair.Key, pair.Value);
    Console.ReadKey();
}
查看更多
Juvenile、少年°
5楼-- · 2019-01-17 16:31

Here is an answer that avoids using Dictionaries. Since the OP said he is not familiar with them, this might give him a little insight into what Dictionaries do.

The downside to this answer is you have to enforce a limit on the max number in the array, and you can't have negative numbers. You'd never actually use this version in real code.

int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
int[] count = new int[13];

foreach(int number in array) {
    // using the index of count same way you'd use a key in a dictionary
    count[number]++;
}

foreach(int c in count) {
    int numberCount = count[c];
    if(numberCount > 0) {
        Console.WriteLine(c + " occurs " + numberCount + " times");
    }
}
查看更多
三岁会撩人
6楼-- · 2019-01-17 16:32

I'm in agreement that using Dictionary is better running time performance then nested for loops (O(n) vs O(n^2)). However to address OP, here is a solution where a HashSet is used to prevent repeating the counting of integers already counted, such as integer 5 in example array.

static void Main(string[] args)
{              
    int[] A = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };

    var set = new HashSet<int>();
    for (int i = 0; i < A.Length - 1; i++) {
        int count = 0;
        for (int j = i; j < A.Length - 1; j++) {
            if (A[i] == A[j + 1] && !set.Contains(A[i]))
                count++;
        }
        set.Add(A[i]);
        if (count > 0) {
            Console.WriteLine("{0} occurs {1} times", A[i], count + 1);
            Console.ReadKey();
        }
    }
}
查看更多
登录 后发表回答