From a7928f50dd2a39b0941ac7e15061d65393d23a86 Mon Sep 17 00:00:00 2001
From: Mrinal Chauhan <mrinalchauhan18.com>
Date: Wed, 23 Oct 2024 22:45:00 +0530
Subject: [PATCH 1/5] feat : added find majority element in array using Moore's
 Voting Algorithm

---
 Data-Structures/Array/MooreVotingAlgorithm.js | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 Data-Structures/Array/MooreVotingAlgorithm.js

diff --git a/Data-Structures/Array/MooreVotingAlgorithm.js b/Data-Structures/Array/MooreVotingAlgorithm.js
new file mode 100644
index 0000000000..50fd2350a4
--- /dev/null
+++ b/Data-Structures/Array/MooreVotingAlgorithm.js
@@ -0,0 +1,33 @@
+/**
+ * Moore Voting Algorithm to find the majority element in an array
+ * Majority element is the one that appears more than n/2 times
+ * geeksforgeeks: https://www.geeksforgeeks.org/boyer-moore-majority-voting-algorithm/
+ * @param {Array} arr array of integers
+ * @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--;
+      }
+    }
+  
+    // Phase 2: Validate the candidate
+    count = 0;
+    for (let num of arr) {
+      if (num === candidate) {
+        count++;
+      }
+    }
+  
+    return count > arr.length / 2 ? candidate : null;
+  };
\ No newline at end of file

From 8d44461767b586972aad70d643fe6d4e8783f52a Mon Sep 17 00:00:00 2001
From: Mrinal Chauhan <mrinalchauhan18.com>
Date: Wed, 23 Oct 2024 22:46:23 +0530
Subject: [PATCH 2/5] feat : added find majority element in array using Moore's
 Voting Algorithm test file

---
 .../Array/test/MooreVotingAlgorithm.test.js          | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 Data-Structures/Array/test/MooreVotingAlgorithm.test.js

diff --git a/Data-Structures/Array/test/MooreVotingAlgorithm.test.js b/Data-Structures/Array/test/MooreVotingAlgorithm.test.js
new file mode 100644
index 0000000000..0a1effb429
--- /dev/null
+++ b/Data-Structures/Array/test/MooreVotingAlgorithm.test.js
@@ -0,0 +1,12 @@
+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

From 448415cf0c6a0fc38b6a7b8cf4d093aef2ccd10b Mon Sep 17 00:00:00 2001
From: Mrinal Chauhan <mrinalchauhan18.com>
Date: Wed, 23 Oct 2024 22:58:24 +0530
Subject: [PATCH 3/5] feat: added find majority element in array using Moore's
 Voting Algorithm

---
 Data-Structures/Array/MooreVotingAlgorithm.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Data-Structures/Array/MooreVotingAlgorithm.js b/Data-Structures/Array/MooreVotingAlgorithm.js
index 50fd2350a4..bb8b3f8d29 100644
--- a/Data-Structures/Array/MooreVotingAlgorithm.js
+++ b/Data-Structures/Array/MooreVotingAlgorithm.js
@@ -30,4 +30,5 @@ const MooreVotingAlgorithm = (arr) => {
     }
   
     return count > arr.length / 2 ? candidate : null;
-  };
\ No newline at end of file
+  };
+  export { MooreVotingAlgorithm };
\ No newline at end of file

From 193763d5aa7ef8cd6e8bb611ca5c0d5b54d95f70 Mon Sep 17 00:00:00 2001
From: Mrinal Chauhan <mrinalchauhan18.com>
Date: Fri, 25 Oct 2024 03:19:15 +0530
Subject: [PATCH 4/5] fix: updated CONTRIBUTING.md and added test cases for
 Moore Voting Algorithm

---
 .husky/pre-commit                             |  0
 DIRECTORY.md                                  |  1 +
 Data-Structures/Array/MooreVotingAlgorithm.js | 43 ++++++++-----------
 .../Array/test/MooreVotingAlgorithm.test.js   | 21 +++++----
 Maths/MobiusFunction.js                       |  4 +-
 package-lock.json                             |  6 ++-
 package.json                                  |  6 +--
 7 files changed, 39 insertions(+), 42 deletions(-)
 mode change 100755 => 100644 .husky/pre-commit

diff --git a/.husky/pre-commit b/.husky/pre-commit
old mode 100755
new mode 100644
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..1682c25089 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
+    [[1, 2, 3, 4, 5, 6, 7], null] // No majority element in the array
+  ])('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"

From 1951f32f2bc36fa6588551dedcefb6e8df5d09ff Mon Sep 17 00:00:00 2001
From: Mrinal Chauhan <mrinalchauhan18.com>
Date: Fri, 25 Oct 2024 11:57:32 +0530
Subject: [PATCH 5/5] fix: updated code

---
 Maths/MobiusFunction.js | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/Maths/MobiusFunction.js b/Maths/MobiusFunction.js
index 4239d6ab31..4bc1b33f5c 100644
--- a/Maths/MobiusFunction.js
+++ b/Maths/MobiusFunction.js
@@ -18,16 +18,19 @@
  * @param {Integer} number
  * @returns {Integer}
  */
-
 import { PrimeFactors } from './PrimeFactors.js'
+
 export const mobiusFunction = (number) => {
-  const primeFactorsArray = PrimeFactors(number)
   if (number <= 0) {
     throw new Error('Number must be greater than zero.')
   }
-  return primeFactorsArray.length !== new Set(primeFactorsArray).size
-    ? 0
-    : primeFactorsArray.length % 2 === 0
-      ? 1
-      : -1
+
+  const primeFactorsArray = PrimeFactors(number)
+  const uniquePrimeFactors = new Set(primeFactorsArray)
+
+  // If there are duplicate factors, it means number is not square-free.
+  if (primeFactorsArray.length !== uniquePrimeFactors.size) {
+    return 0
+  }
+  return uniquePrimeFactors.size % 2 === 0 ? 1 : -1
 }