数组中,有一个元素的值在数组中重复的个数是超过一半,获得元素超过一半的元素值
int
MoreThanHalfNumber(
int
*
arr
,
int
n
){
int
result =
arr
[0];
int
time = 1;
for
(
int
i = 1; i <
n
; i++){
if
(time == 0){
result =
arr
[i];
time = 1;
}
else
if
(
arr
[i] == result){
time++;
}
else
{
time--;
}
}
return
result;
}