C++数组中有一个数目字出现的次数超过数组长度的一半,请找出这个数字。(牛客剑指offer)

C++数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。(牛客剑指offer)
/////////////////////////////////////////////////////////
//数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。
//例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。
//由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。
#include <iostream>
using namespace std;
int Grial(int a[],int n)
{
	if(n==0)return -1;
	int flog = n/2;
	int i = 0;
	int count = 0;
	for(;i<n;i++)
	{	
		for(int j =i;j<n;j++)
		{
			if(a[i]==a[j])count++;
		}
		if(count>flog)return a[i];
		else count = 0;
	}
	return 0;
}

int main()
{
	int a[]={1,1,1,12,3};
	cout<<Grial(a,5)<<endl;
	return 0;
}