# C++ – Divide-and-conquer algorithm for finding the majority element

Posted on

algorithm, c++

An array is said to have a majority element if more than half of its elements are the same. Is there a divide-and-conquer algorithm for determining if an array has a majority element?

I normally do the following, but it is not using divide-and-conquer. I do not want to use the Boyer-Moore algorithm.

`int find(int[] arr, int size) { int count = 0, i, mElement; for (i = 0; i < size; i++) { if (count == 0) mElement = arr[i]; if (arr[i] == mElement) count++; else count--; } count = 0; for (i = 0; i < size; i++) { if (arr[i] == mElement) count++; } if (count > size / 2) return mElement; return -1;}`

## Related posts

- C++ – Divide-and-conquer algorithm for finding the majority element
- Java – the algorithm for finding the center of a circle from three points
- Ny super fast algorithm for finding LINES on picture
- Algorithm for finding out pixel coordinates on a circumference of a circle
- Peak finding algorithm
- C++ – Finding C++ interval tree algorithm implementation
- R – Finding a fuzzy match with bitap algorithm
- Graph algorithm finding if graph is connected, bipartite, has cycle and is a tree
- Simple algorithm for finding maximum and minimum of given values
- C++ – Algorithm for finding the smallest power of two that’s greater or equal to a given value
- Greedy algorithm for finding minimum numbers of stops
- R – How to find Eulerian paths using the cycle finding algorithm
- Javascript – Algorithm for finding overlapping events/times
- Algorithm for finding nearest object on 2D grid
- C# – Algorithm for finding a point in an irregular polygon
- Java – Divide and Conquer Closest Pair Algorithm
- Python – Finding words from random input letters in python. What algorithm to use/code already there
- Finding MST of directed graph using Prim’s algorithm
- R – Algorithm for finding failure cases in a communication “web”
- Random contraction algorithm for finding Min Cuts in a graph
- Majority vote algorithm in Weka.classifiers.meta.vote
- Image comparison algorithm for finding the most similar image in a set
- C++ – What’s time complexity of this algorithm for finding all combinations
- Polynomial time algorithm for finding a Hamiltonian walk in a graph
- Finding Accuracy of a Clustering Algorithm
- Algorithm for finding smallest number with given number of factors
- C++ – Algorithm for finding all cycles in a directed graph on C++ using Adjacency matrix
- Algorithm for finding a Hamilton Path in a DAG
- C# – Algorithm for finding a group of numbers in a list that equal a target
- Java – algorithm for finding longest sequence of the same element in 1D array-looking for better solution
- Opencv – the algorithm that opencv uses for finding contours
- Python – Algorithm to Divide a list of numbers into 2 equal sum lists
- Finding edge connectivity of a network by using Maximum Flow algorithm
- Php – Algorithm for Finding Good, Reliable Players
- Linear algorithm of finding tree diameter
- Fastest algorithm for finding a word on a word search grid
- C# – Efficient algorithm for finding all keywords in a text
- JavaScript – Improving algorithm for finding square roots of perfect squares without Math.sqrt
- Javascript – a good 2D grid-based path-finding algorithm?
- C# – Algorithm for finding the segment overlapping two collinear segments
- Power with divide & conquer algorithm
- R – Algorithm for finding all combinations of k elements from stack of n “randomly”
- Algorithm to divide a chocolate bar in equal parts
- C++ – Floyd’s cycle-finding algorithm
- C++ – Algorithm for finding the number which appears the most in a row – C++
- R – Algorithm for finding potential matches
- C++ – Algorithm for finding path to point
- Java – Parallelizing Sieve of Eratosthenes Algorithm for finding Prime Number
- R – Need Better Algorithm for Finding Mapping Between 2 Sets of Points with Minimum Distance
- Algorithm for finding the ratio of two floating-point numbers
- The best algorithm for finding the closest color in an array to another color?
- R – How does space partitioning algorithm for finding nearest neighbors work
- C# – Efficient algorithm for finding related submissions
- Fast algorithm for finding prime numbers?
- C++ –
function for finding last item less-than-or-equal to, like lower_bound - Iphone – Compilation Error on #include
Algorithm:No such file or Directory - An easy way for finding C and N when proving the Big-Oh of an Algorithm
- Algorithm for finding nearby points
- O(nlogn) divide and conquer algorithm to find visible lines
- The dynamic programming algorithm for finding a Hamiltonian cycle in a graph
- How to use Chamfer Matching algorithm for finding ‘Similar Images’
- C++ – Implementing De Boors algorithm for finding points on a B-spline
- Ideal algorithm for finding all paths in a graph
- R – Algorithm for finding total idle time given array of start/end times
- O(log n) algorithm for finding max of array
- Python – Right algorithm for finding largest prime factor
- R – “e” variable in popular implementations of Brent root finding algorithm
- Java – Divide and Conquer algorithm in java
- Python – Algorithm for finding if an array is balanced
- Algorithm for finding visually similar photos from a database
- Looking for algorithm finding euler path
- Finding the average case complexity for an algorithm
- Explanation of Algorithm for finding articulation points or cut vertices of a graph
- R – k partition algorithm – divide work load equally between k workers
- C# – Finding the shortest route using Dijkstra algorithm
- R – Algorithm for finding out the cheapst combination
- Why is Binary Search a divide and conquer algorithm
- Divide algorithm – time complexity
- Java – Algorithm for finding all paths in a NxN grid
- Java – Algorithm for finding a prime with the least amount of computations
- R – Fast algorithm for finding next smallest and largest number in a set
- Algorithm for finding numerical permutation given lexicographic index
- Integer multiplication algorithm using a divide and conquer approach
- Randomized algorithm for finding hamiltonian path in a directed graph
- R – Algorithm for multidimensional optimization / root-finding / something
- Algorithm for Finding Redundant Edges in a Graph or Tree
- Good algorithm for finding the diameter of a (sparse) graph
- Algorithm for finding the fewest rectangles to cover a set of rectangles without overlapping
- What are some good methods to finding a heuristic for the A* algorithm
- C# – Algorithm for finding the difference between two arrays
- C++ – Divide and conquer algorithm for sum of integer array
- Algorithm for finding similar images
- Divide and conquer algorithm for finding the smallest value in the array
- Java Algorithm for finding the largest set of independent nodes in a binary tree
- A divide-and-conquer algorithm for counting dominating points
- Why is greedy algorithm not finding maximum independent set of a graph
- Optimal Algorithm needed for finding pairs divisible by a given integer k
- Bron-Kerbosch algorithm for clique finding
- C++ – Algorithm for finding the maximum difference in an array of numbers
- R – On Path Finding: a detailed description for a layman of the D* algorithm

## Best Solution

I can see at least one divide and conquer method.

Start by finding the median, such as with Hoare's Select algorithm. If one value forms a majority of the elements, the median must have that value, so we've just found the value we're looking for.

From there, find (for example) the 25th and 75th percentile items. Again, if there's a majority element, at least one of those would need to have the same value as the median.

Assuming you haven't ruled out there being a majority element yet, you can continue the search. For example, let's assume the 75th percentile was equal to the median, but the 25th percentile wasn't.

When then continue searching for the item halfway between the 25th percentile and the median, as well as the one halfway between the 75th percentile and the end.

Continue finding the median of each partition that must contain the end of the elements with the same value as the median until you've either confirmed or denied the existence of a majority element.

As an aside: I don't quite see how Boyer-Moore would be used for this task. Boyer-Moore is a way of finding a substring in a string.