Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Moore's Voting Algorithm - Boyer-Moore Majority Voting Algorithm #1750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mrinalchauhan wants to merge 5 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from mrinalchauhan:master
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .husky/pre-commit
100755 → 100644
View file Open in desktop
Empty file.
1 change: 1 addition & 0 deletions DIRECTORY.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* [NumberOfLocalMaximumPoints](Data-Structures/Array/NumberOfLocalMaximumPoints.js)
* [QuickSelect](Data-Structures/Array/QuickSelect.js)
* [Reverse](Data-Structures/Array/Reverse.js)
* [MooreVotingAlgorithm](Data-Structures/Array/MooreVotingAlgorithm.js)
* **Graph**
* [Graph](Data-Structures/Graph/Graph.js)
* [Graph2](Data-Structures/Graph/Graph2.js)
Expand Down
29 changes: 29 additions & 0 deletions Data-Structures/Array/MooreVotingAlgorithm.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Moore Voting Algorithm to find the majority element in an array
* Majority element is the one that appears more than n/2 times
* geeksforgeeks: https://www.geeksforgeeks.org/boyer-moore-majority-voting-algorithm/
* @param {Array} arr array of integers
* @returns {Number} majority element or null if no majority exists
*/
const MooreVotingAlgorithm = (arr) => {
let candidate = null
let count = 0

// Phase 1: Find the candidate for majority element
for (let num of arr) {
if (count === 0) {
candidate = num
}
count += num === candidate ? 1 : -1
}

// Phase 2: Verify if the candidate is actually the majority element
count = 0
for (let num of arr) {
if (num === candidate) {
count++
}
}
return count > arr.length / 2 ? candidate : null
}
export { MooreVotingAlgorithm }
11 changes: 11 additions & 0 deletions Data-Structures/Array/test/MooreVotingAlgorithm.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MooreVotingAlgorithm } from '../MooreVotingAlgorithm'
describe('Moore Voting Algorithm', () => {
it.each([
[[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
[[2, 2, 2, 2, 5, 5, 5, 2], 2], // Majority element 2
[[3], 3], // Single element, it's the majority
[[1, 2, 3, 4, 5, 6, 7], null] // No majority element in the array
])('returns %j when given %j', (array, expected) => {
expect(MooreVotingAlgorithm(array)).toEqual(expected)
})
})
17 changes: 10 additions & 7 deletions Maths/MobiusFunction.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
* @param {Integer} number
* @returns {Integer}
*/

import { PrimeFactors } from './PrimeFactors.js'

export const mobiusFunction = (number) => {
const primeFactorsArray = PrimeFactors(number)
if (number <= 0) {
throw new Error('Number must be greater than zero.')
}
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1

const primeFactorsArray = PrimeFactors(number)
const uniquePrimeFactors = new Set(primeFactorsArray)

// If there are duplicate factors, it means number is not square-free.
if (primeFactorsArray.length !== uniquePrimeFactors.size) {
return 0
}
return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
}
6 changes: 4 additions & 2 deletions package-lock.json
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"author": "TheAlgorithms",
"license": "GPL-3.0",
"devDependencies": {
"@vitest/coverage-v8": "^1.2.1",
"globby": "^13.2.2",
"husky": "^8.0.3",
"prettier": "^3.0.3",
"vitest": "^1.2.1",
"@vitest/coverage-v8": "^1.2.1"
"prettier": "^3.3.3",
"vitest": "^1.2.1"
},
"engines": {
"node": ">=20.6.0"
Expand Down

AltStyle によって変換されたページ (->オリジナル) /