C++: Fastest method to check if all array elements are equal

后端 未结 10 1987
庸人自扰
庸人自扰 2020-12-01 12:50

What is the fastest method to check if all elements of an array(preferable integer array) are equal. Till now I have been using the following code:

bool chec         


        
10条回答
  •  眼角桃花
    2020-12-01 13:30

    We'll it's basically an O(n) operation so you can't do much better than what you have, other than dispensing with the flag and just return false; on the first failure and return true; after the iteration.

提交回复
热议问题