简体   繁体   中英

Output how many duplicates in an array

I'm looking to output the number of duplicates for each int in an array eg 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]]);
        }
    }

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]]++;

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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM