diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 83f0624254..5a31587a20 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -66,13 +66,13 @@ should add a unique value. Examples of best commit messages. -```txt +````txt fix: fixed error in XYZ algorithm feat: re-work the CI workflow docs: improve the contributing guidelines test: add self-tests for XYZ algorithm chore: update readme badges -``` +`` #### File Naming Convention @@ -107,7 +107,7 @@ First, you should install all dependencies using: ```bash npm install -``` +```` You can (and should!) run all tests locally before committing your changes: diff --git a/DIRECTORY.md b/DIRECTORY.md index 7f6484cae5..c1c7916d75 100644 --- a/DIRECTORY.md +++ b/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) * [Graph2](Data-Structures/Graph/Graph2.js) diff --git a/Data-Structures/Array/MooreVotingAlgorithm.js b/Data-Structures/Array/MooreVotingAlgorithm.js index bb8b3f8d29..17ae0881ba 100644 --- a/Data-Structures/Array/MooreVotingAlgorithm.js +++ b/Data-Structures/Array/MooreVotingAlgorithm.js @@ -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 }; \ No newline at end of file + } + return count > arr.length / 2 ? candidate : null +} +export { MooreVotingAlgorithm } diff --git a/Data-Structures/Array/test/MooreVotingAlgorithm.test.js b/Data-Structures/Array/test/MooreVotingAlgorithm.test.js index 0a1effb429..615a790115 100644 --- a/Data-Structures/Array/test/MooreVotingAlgorithm.test.js +++ b/Data-Structures/Array/test/MooreVotingAlgorithm.test.js @@ -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); - }); - }); \ No newline at end of file + 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) + }) +}) diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js index bd268b8bbd..4239d6ab31 100644 --- a/Maths/MobiusFunction.js +++ b/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 } diff --git a/package-lock.json b/package-lock.json index 5c38ba06a8..58e15a8c63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@vitest/coverage-v8": "^1.2.1", "globby": "^13.2.2", "husky": "^8.0.3", - "prettier": "^3.0.3", + "prettier": "^3.3.3", "vitest": "^1.2.1" }, "engines": { @@ -1662,7 +1662,9 @@ } }, "node_modules/prettier": { - "version": "3.0.3", + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.3.3.tgz", + "integrity": "sha512-i2tDNA0O5IrMO757lfrdQZCc2jPNDVntV0m/+4whiDfWaTKfMNgR7Qz0NAeGz/nRqF4m5/6CLzbP4/liHt12Ew==", "dev": true, "license": "MIT", "bin": { diff --git a/package.json b/package.json index e667e1eea2..122e9952bf 100644 --- a/package.json +++ b/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"