How to check if all numbers in an array are equal in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    public static bool oneUniqueElement(int []arr) {
        HashSet<int> set = new HashSet<int>();
     
        for (int i = 0; i < arr.Length; i++) {
            set.Add(arr[i]);
        }
     
        if (set.Count == 1)
            return true;

        return false;
    }
    static void Main() {
        int []arr = {5, 5, 5, 5, 5, 5, 5};

        if (oneUniqueElement(arr))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}




/*
run:
            
Yes
            
*/

 



answered Dec 6, 2021 by avibootz

Related questions

...