Comparing 5 Integers in least number of comparisons
algorithm, c#
Solution
It's just about possible that there's some hacky/clever bit-twiddling way of doing this with XORs or something - but your code makes it clear what you want to do, and will still be ridiculously fast. The chances of this becoming a bottleneck are close enough to 0 to not be worth considering IMO - so go with the most readable code.
I would be a bit more consistent in your comparisons though:
bool allEqual = (result1 == result2) &&
(result1 == result3) &&
(result1 == result4) &&
(result1 == result5);
It's easier to see visually that you've got all the bases covered, IMO.
Problem
I have this code and need to make sure that all result variables are equal? ``` long result1 = timer.CalculateElapsedTimeInMinutes(); long result2 = timer.CalculateElapsedTimeInMinutes(); long result3 = timer.CalculateElapsedTimeInMinutes(); long result4 = timer.CalculateElapsedTimeInMinutes(); long result5 = timer.CalculateElapsedTimeInMinutes(); ``` This is what I did, but I feel like it can be done in a simpler way, maybe not? ``` bool allEqual = (result1 == result2) && (result3 == result4) && (result1 == result3) && (result1 == result5); ``` Thanks.