Skip to content

Commit 193763d

Browse files
author
Mrinal Chauhan
committed
fix: updated CONTRIBUTING.md and added test cases for Moore Voting Algorithm
1 parent 448415c commit 193763d

File tree

7 files changed

+39
-42
lines changed

7 files changed

+39
-42
lines changed

.husky/pre-commit

100755100644
File mode changed.

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* [NumberOfLocalMaximumPoints](Data-Structures/Array/NumberOfLocalMaximumPoints.js)
7474
* [QuickSelect](Data-Structures/Array/QuickSelect.js)
7575
* [Reverse](Data-Structures/Array/Reverse.js)
76+
* [MooreVotingAlgorithm](Data-Structures/Array/MooreVotingAlgorithm.js)
7677
* **Graph**
7778
* [Graph](Data-Structures/Graph/Graph.js)
7879
* [Graph2](Data-Structures/Graph/Graph2.js)

Data-Structures/Array/MooreVotingAlgorithm.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,24 @@
66
* @returns {Number} majority element or null if no majority exists
77
*/
88
const MooreVotingAlgorithm = (arr) => {
9-
let candidate = null;
10-
let count = 0;
11-
12-
// Phase 1: Finding the candidate
13-
for (let num of arr) {
14-
if (count === 0) {
15-
candidate = num;
16-
count = 1;
17-
} else if (num === candidate) {
18-
count++;
19-
} else {
20-
count--;
21-
}
9+
let candidate = null
10+
let count = 0
11+
12+
// Phase 1: Find the candidate for majority element
13+
for (let num of arr) {
14+
if (count === 0) {
15+
candidate = num
2216
}
23-
24-
// Phase 2: Validate the candidate
25-
count = 0;
26-
for (let num of arr) {
27-
if (num === candidate) {
28-
count++;
29-
}
17+
count += num === candidate ? 1 : -1
18+
}
19+
20+
// Phase 2: Verify if the candidate is actually the majority element
21+
count = 0
22+
for (let num of arr) {
23+
if (num === candidate) {
24+
count++
3025
}
31-
32-
return count > arr.length / 2 ? candidate : null;
33-
};
34-
export { MooreVotingAlgorithm };
26+
}
27+
return count > arr.length / 2 ? candidate : null
28+
}
29+
export { MooreVotingAlgorithm }
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import {MooreVotingAlgorithm} from "../MooreVotingAlgorithm";
1+
import { MooreVotingAlgorithm } from '../MooreVotingAlgorithm'
22
describe('Moore Voting Algorithm', () => {
3-
it.each([
4-
[[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
5-
[[1, 2, 3, 4], null], // No majority element
6-
[[2, 2, 2, 2, 5, 5, 5, 2], 2], // Majority element 2
7-
[[], null], // Empty array, no majority
8-
[[3], 3] // Single element, it's the majority
9-
])('returns %j when given %j', (array, expected) => {
10-
expect(MooreVotingAlgorithm(array)).toEqual(expected);
11-
});
12-
});
3+
it.each([
4+
[[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
5+
[[2, 2, 2, 2, 5, 5, 5, 2], 2], // Majority element 2
6+
[[3], 3], // Single element, it's the majority
7+
[[1, 2, 3, 4, 5, 6, 7], null] // No majority element in the array
8+
])('returns %j when given %j', (array, expected) => {
9+
expect(MooreVotingAlgorithm(array)).toEqual(expected)
10+
})
11+
})

Maths/MobiusFunction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
2828
return primeFactorsArray.length !== new Set(primeFactorsArray).size
2929
? 0
3030
: primeFactorsArray.length % 2 === 0
31-
? 1
32-
: -1
31+
? 1
32+
: -1
3333
}

package-lock.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"author": "TheAlgorithms",
1414
"license": "GPL-3.0",
1515
"devDependencies": {
16+
"@vitest/coverage-v8": "^1.2.1",
1617
"globby": "^13.2.2",
1718
"husky": "^8.0.3",
18-
"prettier": "^3.0.3",
19-
"vitest": "^1.2.1",
20-
"@vitest/coverage-v8": "^1.2.1"
19+
"prettier": "^3.3.3",
20+
"vitest": "^1.2.1"
2121
},
2222
"engines": {
2323
"node": ">=20.6.0"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy