O(n+logN): 排序后+统计
O(1+lonN):排序后+查找中间元素(大于一半的元素一定出现在有序序列的中点)
O(N):不同元素两两相消
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 3 using namespace std; 4 5 6 int find(int array[], int N) 7 { 8 int candid, ntimes; 9 10 ntimes=0; 11 for(int i=0; i<N; i++) 12 { 13 if(ntimes==0) 14 { 15 candid = array[i]; 16 ntimes=1; 17 } 18 else 19 { 20 if(array[i] == candid) 21 { 22 ntimes++; 23 } 24 else 25 { 26 ntimes--; 27 } 28 } 29 } 30 return candid; 31 } 32 33 int main() 34 { 35 int array[6]={1,2,0,1,1,1}; 36 int rlt; 37 38 rlt=find(array,6); 39 cout<<rlt<<endl; 40 41 return 0; 42 }