Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 448415c commit 41f16e4Copy full SHA for 41f16e4
CONTRIBUTING.md
@@ -66,13 +66,13 @@ should add a unique value.
66
67
Examples of best commit messages.
68
69
-```txt
+````txt
70
fix: fixed error in XYZ algorithm
71
feat: re-work the CI workflow
72
docs: improve the contributing guidelines
73
test: add self-tests for XYZ algorithm
74
chore: update readme badges
75
-```
+``
76
77
#### File Naming Convention
78
@@ -107,7 +107,7 @@ First, you should install all dependencies using:
107
108
```bash
109
npm install
110
+````
111
112
You can (and should!) run all tests locally before committing your changes:
113
DIRECTORY.md
@@ -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)
79
* [Graph2](Data-Structures/Graph/Graph2.js)
Data-Structures/Array/MooreVotingAlgorithm.js
@@ -6,29 +6,24 @@
6
* @returns {Number} majority element or null if no majority exists
7
*/
8
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
- }
+ let candidate = null
+ let count = 0
+
+ // Phase 1: Find the candidate for majority element
+ for (let num of arr) {
+ if (count === 0) {
+ candidate = num
22
}
23
24
- // Phase 2: Validate the candidate
25
- count = 0;
26
27
- if (num === candidate) {
28
29
+ count += num === candidate ? 1 : -1
+ }
+ // Phase 2: Verify if the candidate is actually the majority element
+ count = 0
+ if (num === candidate) {
+ count++
30
31
32
- return count > arr.length / 2 ? candidate : null;
33
- };
34
- export { MooreVotingAlgorithm };
+ return count > arr.length / 2 ? candidate : null
+}
+export { MooreVotingAlgorithm }
Data-Structures/Array/test/MooreVotingAlgorithm.test.js
@@ -1,12 +1,11 @@
1
-import {MooreVotingAlgorithm} from "../MooreVotingAlgorithm";
+import { MooreVotingAlgorithm } from '../MooreVotingAlgorithm'
2
3
describe('Moore Voting Algorithm', () => {
- it.each([
4
- [[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
5
- [[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
+ ])('returns %j when given %j', (array, expected) => {
+ expect(MooreVotingAlgorithm(array)).toEqual(expected)
+ })
+})
Maths/MobiusFunction.js
@@ -28,6 +28,6 @@ export const mobiusFunction = (number) => {
return primeFactorsArray.length !== new Set(primeFactorsArray).size
? 0
: primeFactorsArray.length % 2 === 0
- ? 1
- : -1
+ ? 1
+ : -1
package-lock.json
package.json
@@ -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"
0 commit comments