Skip to content

[FEATURE REQUEST] Add Moore's Voting Algorithm #5636

@SubhradeepBasu18

Description

@SubhradeepBasu18

What would you like to Propose?

The problem is to find the majority element in an array using Moore's Voting Algorithm. A majority element is an element that appears more than n/2 times in the array, where n is the total number of elements. Moore's Voting Algorithm efficiently solves this problem in O(n) time and O(1) space.

Example:
Input: [2, 2, 1, 1, 1, 2, 2]
Output: 2

Explanation:

  • In the first phase, the algorithm selects 2 as the majority candidate.
  • In the second phase, the algorithm checks if 2 appears more than 3 times (7/2 = 3.5), which it does (appears 4 times), making it the majority element.

Issue details

The algorithm works by:

  • Selecting a candidate element using a voting process.
  • Verifying whether the candidate is the actual majority element by counting its occurrences.

Additional Information

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions