-1

I'm looking to output the number of duplicates for each int in an array e.g. the array 1,2,3,4,1,1,3 would 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.

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]]);
        }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135

4 Answers4

2

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. In your case a GroupBy should already do the trick.

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. c++ and ++c are different operations, this is described in What is the difference between ++i and 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]]++;
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
0

If you want to use 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);
Eser
  • 12,346
  • 1
  • 22
  • 32
0

While Fruchtzwerg is correct, you're returning c before it's incremented, you shouldn't even be making a temp variable.

Just do:

dict[arr1[i]] += 1; //or dict[arr1[i]]++;

You also shouldn't use that for-loop to loop through the dictionary values. 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

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
-2

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;
    }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
vishal
  • 1
  • 5
    This answer adds nothing new that hasn't already been covered by other answers. – Kent Kostelac Apr 20 '22 at 10:11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 00:06