rootUser

Find Majority element from an integer array

May 24th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. /* Find Majority element of an array. Majority elements are those which number appears more than half of the array size.
  2.  
  3. Sample input: 2 2 3 4 2
  4. Sample output: 2
  5.  
  6. Sample input: 3 4 3 7 8 9
  7. Sample output: none
  8. */
  9.  
  10. #include<stdio.h>
  11. int main(void)
  12. {
  13.     int x,p,i,j;
  14.     printf("enter array size:\n");
  15.     scanf("%d",&x);
  16.     p=x/2;
  17.     int a[x];
  18.     for(i=0;i<x;i++)
  19.     {
  20.         scanf("%d",&a[i]);
  21.     }
  22.     for(i=0;i<x;i++)
  23.     {
  24.         int counter=0;
  25.         for(j=0;j<x;j++)
  26.         {
  27.             if(a[i]==a[j])
  28.             {
  29.                 counter=counter+1;
  30.             }
  31.         }
  32.         if(counter>p)
  33.         {
  34.             printf("%d",a[i]);
  35.             break;
  36.         }
  37.     }
  38.  
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment