-4

I have 3 arrays and I would like to pass an if statement if only 1 of the 3 arrays are not empty. I only want to pass it if only 1 is not empty and the other 2 are empty.

Right now I have a crazy if statement and was wondering if it can be simplified

if((a && !b && !c) || (!a && b && !c) || (!a && !b && c))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Joe
  • 1,047
  • 1
  • 13
  • 25
  • 1
    `var bob = new int[] { a, b, c}; var oneMatch = bob.Count(z => z.Length > 0) == 1;` Change `int` to whatever your type is. _I am assuming your arrays are called `a`, `b` and `c`._ – mjwills Dec 29 '17 at 02:00
  • 3
    You keep using the word `it` and `it` is very hard to understand what `it` you're referring too. Secondly, you're code doesn't really make sense.. what are `a b c`? They don't make sense if they are arrays. – Erik Philips Dec 29 '17 at 02:01
  • I was thinking, make `a`, `b`, `c` equal to `1` if true, `0` otherwise, then just add them. Check if the sum is `1`. –  Dec 29 '17 at 02:01
  • 7
    Possible duplicate of [XOR of three values](https://stackoverflow.com/questions/3466452/xor-of-three-values) – Kirlac Dec 29 '17 at 02:07
  • Are a, b, and c arrays or bools? – SILENT Dec 29 '17 at 03:50
  • It can be simplified to `a ? !b && !c : b ^ c` but that's not necessarily less crazy. – Wyck Dec 29 '17 at 05:08
  • This is a useful question but I cannot add my answer there. First, you should realize that the input of the functions are three boolean variables and the order of the variables doesn't matter, which means if f(a,b,c) is true then f(b,c,a) or any such others should be also true. This is called symmetric boolean functions. And there are algorithms to generate such functions with the shortest expression length(as you asked: the simplest). – Changming Sun Jul 21 '23 at 04:30

1 Answers1

5

Assuming a, b, and c are boolean values whose values indicate if the corresponding array is empty or not:

(a ^ b ^ c) && !(a && b && c)

If you XOR three boolean values in sequence, it will be true if and only if exactly one variable is true OR if all three variables are true. Hence, the second part of the expression, to eliminate the case where all three variables are true.

ubadub
  • 3,571
  • 21
  • 32