Majority Element in an Array in C

Majority Element in array in C Language

 

In this page we will look into a coding question where we will learn how to find the element which occurs majority number of times in array in C Programming Language.
There might be different approach to solve this question, one you will find here. If your approach is bit different post it onto the comment section.

Majority Element in an Array in C

Majority Element In An Array In C

  • The majority element in an array is one that appears more than n/2 times in an array of size n. There are various algorithms that can be used to find the major element.
  • One such algorithm is Moore’s Voting Algorithm, which works through two-passes; it first finds a potential candidate by counting its frequency, and then verifies it by counting it again on the second pass.
  • Alternatively, one can use a hash table to count each element’s frequency and find the majority element from that.
  • Lastly, sorting the array and checking its middle element can work too, though this has a time complexity of O(nlogn).
  •  Overall, Moore’s Voting Algorithm is regarded as the most efficient given its O(n) time complexity and O(1) space complexity.
Majority Element In An Array

Algorithm

  1. Start
  2. set maxCount=0, Index=-1
  3. Start loop from i=0 to i < n
  4. set count = 0
  5. Start loop from j=0 to j < n
  6. if(arr[i]==arr[j])
  7. count++
  8. End if
  9. End For
  10. Start if (count > maxCount)
  11.  maxCount = count
  12. index = i
  13. End If
  14. End For
  15. Start if (maxCount > n / 2)
  16. print arr[index]
  17. End if
  18. Else print “No Majority Element”
  19. End Else
  20. Stop

Program for finding the element that occurs most time in an array

Run
#include<stdio.h>

void getMajorityElement(int arr[], int n)
{
    int maxCount = 0;
    int index = -1; // sentinels
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j])
                count++;
        }
  
        // update maxCount if count of
        // current element is greater
        if (count > maxCount) {
            maxCount = count;
            index = i;
        }
    }
  
    // if maxCount is greater than n/2
    // return the corresponding element
    if (maxCount > n / 2)
        printf("Majority Element = %d\n",arr[index]);
  
    else
        printf("No Majority Element\n");
}
  
// Driver code
int main()
{
    int arr[] = { 3, 3, 4, 2, 4, 4, 2, 4, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function calling
    getMajorityElement(arr, n);
  
    return 0;
}

Output

Majority Element = 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

One comment on “Majority Element in an Array in C”


  • santhosh

    /********************************************
    #include
    int main()
    {
    int n;
    scanf(“%d”,&n);
    int array[n];
    int max = 0;
    int count;
    int maxelement;
    for(int i=0; i<n; i++)
    {
    scanf("%d",&array[i]);
    }
    for(int i=0; i<n; i++)
    {
    count=0;
    for(int j=0; j1)
    {
    max = count;
    maxelement = array[i];
    }
    }
    printf(“%d”,maxelement);
    return 0;
    }