Movatterモバイル変換


[0]ホーム

URL:


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

Commit193763d

Browse files
author
Mrinal Chauhan
committed
fix: updated CONTRIBUTING.md and added test cases for Moore Voting Algorithm
1 parent448415c commit193763d

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
constMooreVotingAlgorithm=(arr)=>{
9-
letcandidate=null;
10-
letcount=0;
11-
12-
// Phase 1: Finding the candidate
13-
for(letnumofarr){
14-
if(count===0){
15-
candidate=num;
16-
count=1;
17-
}elseif(num===candidate){
18-
count++;
19-
}else{
20-
count--;
21-
}
9+
letcandidate=null
10+
letcount=0
11+
12+
// Phase 1: Find the candidate for majority element
13+
for(letnumofarr){
14+
if(count===0){
15+
candidate=num
2216
}
23-
24-
// Phase 2: Validate the candidate
25-
count=0;
26-
for(letnumofarr){
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(letnumofarr){
23+
if(num===candidate){
24+
count++
3025
}
31-
32-
returncount>arr.length/2 ?candidate :null;
33-
};
34-
export{MooreVotingAlgorithm};
26+
}
27+
returncount>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
returnprimeFactorsArray.length!==newSet(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 aboutcustomizing 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)

[8]ページ先頭

©2009-2025 Movatter.jp