user3711282
user3711282

Reputation: 21

C#: How to check if all the elements in an array of boolean variables are of the same value (All true or All false)?

I have an array of boolean variables and I want to return true if all the elements in this array are of the same value, false otherwise. I know I can loop through all the elements, but I'm wondering if there are any faster ways in C#.

Upvotes: 2

Views: 5869

Answers (7)

Nick Bray
Nick Bray

Reputation: 1963

Just for fun a little different solution:

!(array.Any(b => b) && array.Any(b => !b));

There are two loops here. One of them should exit on the first element in the array. The other one should exit on the first occurrence that is different from the first in the array. It will also return true for empty arrays.

Upvotes: 0

Dennis_E
Dennis_E

Reputation: 8894

Enumerable.Range(0, array.Length-1).All(i => array[i] == array[i+1])

Edit: Fix after comments

Enumerable.Range(1, array.Length).All(i => array[i] == array[0])

Upvotes: 1

Complexity
Complexity

Reputation: 5820

I would go for an extension method. I'd always love those methods:

The class containing the extension method will be:

public static class ExtensionMethods
{
    public static bool AreAll<T>(this T[] source, Func<T, bool> condition)
    { return source.Where(condition).Count() == source.Count(); }

    public static bool AreAllTheSame<T>(this IEnumerable<T> source)
    { return source.Distinct().Count() == 1; }
}

You see that I have 2 extension methods, one taking a Func and one taking no parameter.

The first one is called when you want to check if all the elements in the array has the same value (for example, all elements are true, or all elements are false).

The second one is called, when you don't want to check against a specific parameter, but if you just want to see if all the values are the same.

And than a little demo to demonstrate the extension method itself:

class Program
{
    static void Main(string[] args)
    {
        bool[] array = { true, false, true, false, true };
        bool[] trueArray = { true, true, true, true };

        Console.WriteLine("Searching with a predicate:");
        Console.WriteLine(array.AreAll(x => x).ToString());
        Console.WriteLine(array.AreAll(x => !x).ToString());
        Console.WriteLine(trueArray.AreAll(x => x).ToString());
        Console.WriteLine(trueArray.AreAll(x => !x).ToString());

        Console.WriteLine("Searching without a predicate:");
        Console.WriteLine(array.AreAllTheSame().ToString());
        Console.WriteLine(array.AreAllTheSame().ToString());
        Console.WriteLine(trueArray.AreAllTheSame().ToString());
        Console.WriteLine(trueArray.AreAllTheSame().ToString());

        Console.ReadLine();
    }
}

This will produce the following output:

enter image description here Let's hope it helps.

Upvotes: 0

Steve Whitfield
Steve Whitfield

Reputation: 2029

var allAreTheSame = myArray.Distinct().Count() == 1

Just an alternative to David's approach, slightly shorter and possibly more efficient since I think the enumerator combination will cause the Array to be looped only once.

Upvotes: 0

MarkO
MarkO

Reputation: 2233

var result = array.Distinct().Count() == 1;

Upvotes: 6

ken2k
ken2k

Reputation: 48975

// Assuming the array is NOT empty
// Get first value
var firstValue = myArray.First();

// Check if all other values are identical
var allidentical = myArray.Skip(1).All(z => z == firstValue);

Upvotes: 2

David Pilkington
David Pilkington

Reputation: 13620

var allAreTheSame = myArray.All(a => a) || myArray.All(a => !a)

Upvotes: 7

Related Questions