Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if three ints are all equal

Tags:

c#

Hi say I have three ints: value1, value2 and value3.

How do I best determine if they are all the same?

I tried:

return value1 == value2 == value3

But this said:

Operator '==' cannot be applied to operands of type 'bool' and 'int'.

So I guess it compares the first two which returns a boolean which it tries to compare to the third.

I could go:

return value1 == value2 && value2 == value3;

But this seems to be getting untidy.

Anybody have a good suggestion?

like image 584
AnonyMouse Avatar asked Nov 14 '11 01:11

AnonyMouse


People also ask

How do you know if three numbers are equivalent?

Use the logical AND (&&) operator to check if three values are equal, e.g. if (val1 === val2 && val1 === val3) . For the if block to run, both conditions have to be satisfied, that is, value1 has to be equal to value2 and value3 , which means the 3 values are equal.

How do you compare three integers?

Comparing three integer variables is one of the simplest program you can write at ease. In this program, you can either take input from user using scanf() function or statically define in the program itself. We expect it to be a simple program for you as well.

How do you compare three values?

To compare 3 values, use the logical AND (&&) operator to chain multiple conditions. When using the logical AND (&&) operator, all conditions have to return a truthy value for the if block to run. Copied!

Can you compare 3 numbers in Java?

We can also compare all the three numbers by using the ternary operator in a single statement. If we want to compare three numbers in a single statement, we must use the following statement. In the following program, we have used a single statement to find the largest of three numbers.


3 Answers

I modified my original answer to include a method that is more general purpose and that does not rely on LINQ or extension methods. I think it's safe to assume this method would be more performant based on the fact that it doesn't have to enumerate the entire list to determine uniqueness when there are values that are different early on in the list.

class Program 
{ 
    static void Main(string[] args) 
    { 
        int value1 = 1, value2 = 2, value3 = 1; 
        Console.WriteLine(AllAreEqual<int>(value1, value2, value3));

        Console.Write("V2: 1 value ");
        Console.WriteLine(AllAreEqual_V2<int>(1));

        Console.Write("V2: no value ");
        Console.WriteLine(AllAreEqual_V2<int>());

        Console.Write("V2: 3 values, same ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1, 1));

        Console.Write("V2: 3 values, different ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1, 2));

        Console.Write("V2: 2 values, same ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 1));

        Console.Write("V2: 2 values, different ");
        Console.WriteLine(AllAreEqual_V2<int>(1, 2));

        Console.ReadKey(); 
    } 

    static bool AllAreEqual<T>(params T[] args) 
    { 
        return args.Distinct().ToArray().Length == 1; 
    }

    static bool AllAreEqual_V2<T>(params T[] args)
    {
        if (args.Length == 0 || args.Length == 1)
        {
            return true;
        }

        if (args.Length == 2)
        {
            return args[0].Equals(args[1]);
        }

        T first = args[0];

        for (int index = 1; index < args.Length; index++)
        {
            if (!first.Equals(args[index]))
            {
                return false;
            }
        }

        return true;
    }
}
like image 158
Wil P Avatar answered Oct 31 '22 11:10

Wil P


The second seems just fine to me.

As the list gets longer, that could get unwieldy. In which case I'd write an extension method along the lines of AllSame.

bool AllSame(this IEnumerable<int> list)
{
    bool first = true;
    int comparand = 0;
    foreach (int i in list) {
       if (first) comparand = i;
       else if (i != comparand) return false;
       first = false;
    }
    return true;
}

or use the params keyword:

bool AllSame(params int[] list)
{
    return (list as IEnumerable<int>).AllSame();
}

Then you can just write:

if (AllSame(value1, value2, value3, value4, value5)) ...
like image 20
Ben Voigt Avatar answered Oct 31 '22 12:10

Ben Voigt


That seems fine to me. The only comment I have is that you should introduce an 'explaining variable' for the equation. Besides explaining the calculation, the return now provides a nice place for a breakpoint or a tracepoint when inspecting the result.

bool allThreeAreEqual = value1 == value2 && value2 == value3;
return allThreeAreEqual;
like image 6
Ritch Melton Avatar answered Oct 31 '22 10:10

Ritch Melton