I need to implement a functionality that takes a list of boolean values as input and returns true only if one of the conditions is true (if more, returns false). More formally, f(c1, c2, c3 ... cN) returns true if and only if there's only one condition that evaluates true, otherwise it returns false.
I implemented this method,
boolean trueOnce(boolean[] conditions) {
boolean retval = false;
for (boolean c: conditions) {
if (c) {
if (!retval) {
retval = true;
} else {
retval = false;
break;
}
}
}
return retval;
}
but I'm asking for something more practical. I'm working with Java, but I think this problem is universal for every language. Thanks.
EDIT: The above example does the job very well, I'm only asking for a more practical way.