The question has already been discussed in the link. Find the majority element in array
I know there are more optimized soln's than this but I couldn't understand the approach as discussed below. BTW it's an unsorted array.
Node of the Binary Search Tree (used in this approach) will be as follows.
struct tree
{
int element;
int count;
}BST;
" Insert elements in BST one by one and if an element is already present then increment the count of the node. " At any stage, if count of a node becomes more than n/2 then return. The method works well for the cases where n/2+1 occurrences of the majority element is present in the starting of the array, for example {1, 1, 1, 1, 1, 2, 3, 4}.
If we are passing element one by one to a function, how can we compare if an element is already present or not and increase the count?