alternative way to compare if three ints are equal
Asked Answered
F

5

7
int a = 10;
int b = 10;
int c = 10;

I am trying to find out if there is alternate way to compare if three ints or anything are equal.

The way i am currently doing is

if( (a == b) && (a == c) && (b == c)){

}

I was wondering if there is an alternate and more concise way to do this.

Forwhy answered 12/12, 2013 at 1:23 Comment(1)
For many such ints you might use some set implementation and after adding them check number of elementsDeviant
D
35

Equality is transitive; you don't need the last comparison b == c. If a == b and a == c, then b == c.

Try

if ((a == b) && (a == c)){
Dric answered 12/12, 2013 at 1:25 Comment(0)
M
0

If all you need is to know whether three are equal then you can use:

if ((a==b) && (b==c)) {

}
Macdermot answered 12/12, 2013 at 1:26 Comment(0)
V
0

alternative variant:

int t1 = a-b;
int t2 = c-b;
if ( (t1|t2) == 0 ) // both are equal
Vicious answered 12/12, 2013 at 1:27 Comment(8)
This isn't really more compact than the other answers here, and is certainly less clear ;)Amparoampelopsis
@OliCharlesworth it is not clear, but it is "alternative" :) also, can be a little bit faster (sometimes, at least for c/c++)Imperturbation
Are you saying that 0/0 equals 0 ?Macdermot
@PM77-1: No, but 0|0 certainly does.Amparoampelopsis
@PM77-1 I'm not using division, I'm using binary OR, 0|0 = 0 in deedImperturbation
if (0 == ((a - b) | (b - c))) is equivalent, more compact, but even less clear. (Edit because didn't see Java tag)Nyala
@OliCharlesworth - I guess it's time to put my reading glasses on.Macdermot
@Nyala for java you need to add == 0 or != 0 as it cannot cast int to bool automatically, for c/c++ it is of course shorter notation, but of course less clear :)Imperturbation
K
0
public static int equal(int a, int b, int c) {
    int count = 0;

    if (a==b && b==c) { 
      count = 3; 
    } else if ((a==b && b!=c) || (a==c && a!=b)) { 
      count = 2; 
    }

    return count;
}
Kal answered 10/11, 2020 at 15:36 Comment(2)
Please add some explanation to your answer such that others can learn from itAnklet
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Consider reading How to Answer and edit your answer to improve it.Welter
L
0

You can add them into a list, then remove duplicates and compare the size of the actual list minus the unique elements

List elements = Arrays.asList(a,b,c);
List distinct = elements.stream().distinct().toList();

return elements.size() == distinct.size() ? 0 : elements.size() - distinct.size() + 1;

distinct list contains one entry of the duplicate so we have to increment with 1 because we already decreased it.

Or use a HashSet that holds only unique elements

List elements = Arrays.asList(a,b,c);
HashSet<ArrayList> set = new HashSet<>();
set.addAll(elements);

return elements.size() == set.size() ? 0 : elements.size() - set.size() + 1;
Lavern answered 8/10, 2023 at 11:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.