Skip to content

Commit 41f16e4

Browse files
author
Mrinal Chauhan
committed
Commit message
1 parent 448415c commit 41f16e4

File tree

7 files changed

+42
-45
lines changed

7 files changed

+42
-45
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ should add a unique value.
6666

6767
Examples of best commit messages.
6868

69-
```txt
69+
````txt
7070
fix: fixed error in XYZ algorithm
7171
feat: re-work the CI workflow
7272
docs: improve the contributing guidelines
7373
test: add self-tests for XYZ algorithm
7474
chore: update readme badges
75-
```
75+
``
7676
7777
#### File Naming Convention
7878
@@ -107,7 +107,7 @@ First, you should install all dependencies using:
107107
108108
```bash
109109
npm install
110-
```
110+
````
111111

112112
You can (and should!) run all tests locally before committing your changes:
113113

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'
2+
23
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-
});
4+
it.each([
5+
[[1, 1, 2, 1, 3, 1, 1], 1], // Majority element 1
6+
[[2, 2, 2, 2, 5, 5, 5, 2], 2], // Majority element 2
7+
[[3], 3] // Single element, it's the majority
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