Skip to content

Commit

Permalink
fix: updated CONTRIBUTING.md and added test cases for Moore Voting Al…
Browse files Browse the repository at this point in the history
…gorithm
  • Loading branch information
Mrinal Chauhan committed Oct 24, 2024
1 parent 448415c commit 193763d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
Empty file modified .husky/pre-commit
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions DIRECTORY.md
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
43 changes: 19 additions & 24 deletions Data-Structures/Array/MooreVotingAlgorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@
* @returns {Number} majority element or null if no majority exists
*/
const MooreVotingAlgorithm = (arr) => {
let candidate = null;
let count = 0;

// Phase 1: Finding the candidate
for (let num of arr) {
if (count === 0) {
candidate = num;
count = 1;
} else if (num === candidate) {
count++;
} else {
count--;
}
let candidate = null
let count = 0

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

// Phase 2: Validate the candidate
count = 0;
for (let num of arr) {
if (num === candidate) {
count++;
}
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 };
}
return count > arr.length / 2 ? candidate : null
}
export { MooreVotingAlgorithm }
21 changes: 10 additions & 11 deletions Data-Structures/Array/test/MooreVotingAlgorithm.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {MooreVotingAlgorithm} from "../MooreVotingAlgorithm";
import { MooreVotingAlgorithm } from '../MooreVotingAlgorithm'
describe('Moore Voting Algorithm', () => {
it.each([
[[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
[[1, 2, 3, 4], null], // No majority element
[[2, 2, 2, 2, 5, 5, 5, 2], 2], // Majority element 2
[[], null], // Empty array, no majority
[[3], 3] // Single element, it's the majority
])('returns %j when given %j', (array, expected) => {
expect(MooreVotingAlgorithm(array)).toEqual(expected);
});
});
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)
})
})
4 changes: 2 additions & 2 deletions Maths/MobiusFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
? 1
: -1
? 1
: -1
}
6 changes: 4 additions & 2 deletions package-lock.json

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
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

0 comments on commit 193763d

Please sign in to comment.