How do I find duplicates in an array and display how many times they occurred?

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)
    {
        // When the key is not found, "count" will be initialized to 0
        dict.TryGetValue(value, out int count);
        dict[value] = count + 1;
    }
    
    foreach(var pair in dict)
        Console.WriteLine("Value {0} occurred {1} times.", pair.Key, pair.Value);
    Console.ReadKey();
}

Leave a Comment