This may be a trivial problem, but I want to learn more about other more clever and efficient ways of solving it.
I have a list of items and each item has a property a
whose value is binary.
- If every item in the list has
a == 0
, then I set a separate variableb = 0
. - If every item in the list has
a == 1
, then I setb = 1
. - If there is a mixture of
a == 0
anda == 1
in the list, then I setb = 2
.
I can use a set to keep track of the types of a
value, such that if there are two items in the set after iterating through the list, then I can set b = 2
, whereas if there is only one item in the set I just retrieve the item (either 0 or 1) and use it to set b
.
Any better way?