vector<int> majorityElement(vector<int>& nums) {
     vector<int> ans;
     vector<int> value(2, 0), cnt(2, 0);
     for (int &x : nums) {
         calculate(value, cnt, x);
     }
     if (isMajority(nums, value[0]))
         ans.push_back(value[0]);
     if (value[0] != value[1] && isMajority(nums, value[1]))
         ans.push_back(value[1]);
     return ans;
 }
Example #2
0
 int majorityElement(vector<int>& num){
   if(num.size() < 1) return -1;
   int candidate = findCandidate(num);
   if(isMajority(num, candidate)){
     return candidate;
   }else{
     return - 1;
   }
 }
int main(){
	int a[]={1,2,3,3,3,3,10};
	int n=sizeof(a)/sizeof(a[0]);
	int x=3;
	if(isMajority(a,n,x))
		printf("\nYes %d is majority element",x);
	else
		printf("\nNo %d is not majority element",x);
	printf("\n");
	return 0;
}
Example #4
0
/* Function to print Majority Element */
void printMajority(int * array, int size)
{
  /* Find the candidate for Majority*/
  int cand = findCandidate(array, size);

  /* Print the candidate if it is Majority*/
  if(isMajority(array, size, cand))
    printf(" %d ", cand);
  else
    printf("NO Majority Element");
}
int main() {
   int arr[] = {1, 2, 3, 3, 3, 3, 3, 3, 4, 9, 10};
   int n = sizeof(arr)/sizeof(arr[0]);
   int x = 3;
   if(isMajority(arr, n, x))
     printf("%d appears more than %d times in arr[]", x, n/2);
   else
    printf("%d does not appear more than %d times in arr[]", x, n/2);
 
   return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::vector<int> vect = {5,5,2,5,5,5,2,8,2,5};
    int dominant = isMajority(vect);

    if(dominant==0)
        std::cout << "NO majority element!" << std::endl;
    else
        std::cout << "Majority element is " << dominant << std::endl;

    return a.exec();
}
Example #7
0
/* Function to print Majority Element */
void printMajority(int a[], int size)
{
  /* Find the candidate for Majority*/
  int cand = findCandidate(a, size);

  /* Print the candidate if it is Majority*/
  if(isMajority(a, size, cand)){
	  *(dram_addr + index_ram) = 0x1 | cand;
	  index_ram++;
  }
  else{
	  *(dram_addr + index_ram) = cand;
	  index_ram++;
  }

}