From 2d98762395e0339ba663afb5e04b55533ef99ce0 Mon Sep 17 00:00:00 2001 From: 0xsatoshi99 <0xsatoshi99@users.noreply.github.com> Date: Thu, 6 Nov 2025 01:51:37 +0100 Subject: [PATCH 1/3] Add Boyer-Moore Majority Vote algorithm --- .../Other/BoyerMooreMajorityVoteTests.cs | 88 +++++++++++++++++++ Algorithms/Other/BoyerMooreMajorityVote.cs | 37 ++++++++ 2 files changed, 125 insertions(+) create mode 100644 Algorithms.Tests/Other/BoyerMooreMajorityVoteTests.cs create mode 100644 Algorithms/Other/BoyerMooreMajorityVote.cs diff --git a/Algorithms.Tests/Other/BoyerMooreMajorityVoteTests.cs b/Algorithms.Tests/Other/BoyerMooreMajorityVoteTests.cs new file mode 100644 index 00000000..90fb74dd --- /dev/null +++ b/Algorithms.Tests/Other/BoyerMooreMajorityVoteTests.cs @@ -0,0 +1,88 @@ +using Algorithms.Other; +using NUnit.Framework; +using FluentAssertions; + +namespace Algorithms.Tests.Other; + +public class BoyerMooreMajorityVoteTests +{ + [Test] + public void FindMajority_SimpleMajority_ReturnsCorrectElement() + { + var nums = new[] { 3, 3, 4, 2, 3, 3, 3 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(3); + } + + [Test] + public void FindMajority_AllSameElements_ReturnsThatElement() + { + var nums = new[] { 5, 5, 5, 5 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(5); + } + + [Test] + public void FindMajority_NoMajority_ReturnsNull() + { + var nums = new[] { 1, 2, 3, 4 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().BeNull(); + } + + [Test] + public void FindMajority_EmptyArray_ReturnsNull() + { + var nums = Array.Empty(); + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().BeNull(); + } + + [Test] + public void FindMajority_NullArray_ReturnsNull() + { + int[]? nums = null; + var result = BoyerMooreMajorityVote.FindMajority(nums!); + result.Should().BeNull(); + } + + [Test] + public void FindMajority_SingleElement_ReturnsThatElement() + { + var nums = new[] { 7 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(7); + } + + [Test] + public void FindMajority_MajorityAtEnd_ReturnsCorrectElement() + { + var nums = new[] { 1, 2, 2, 2, 2 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(2); + } + + [Test] + public void FindMajority_MajorityAtStart_ReturnsCorrectElement() + { + var nums = new[] { 8, 8, 8, 8, 1, 2 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(8); + } + + [Test] + public void FindMajority_NegativeNumbers_ReturnsCorrectElement() + { + var nums = new[] { -1, -1, -1, 2, 2 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().Be(-1); + } + + [Test] + public void FindMajority_ExactlyHalf_ReturnsNull() + { + var nums = new[] { 1, 1, 2, 2 }; + var result = BoyerMooreMajorityVote.FindMajority(nums); + result.Should().BeNull(); + } +} diff --git a/Algorithms/Other/BoyerMooreMajorityVote.cs b/Algorithms/Other/BoyerMooreMajorityVote.cs new file mode 100644 index 00000000..838eb100 --- /dev/null +++ b/Algorithms/Other/BoyerMooreMajorityVote.cs @@ -0,0 +1,37 @@ +namespace Algorithms.Other; + +/// +/// Boyer-Moore Majority Vote algorithm. +/// Finds element appearing more than n/2 times in O(n) time, O(1) space. +/// +public static class BoyerMooreMajorityVote +{ + /// + /// Finds the majority element. + /// + /// Input array. + /// Majority element or null. + public static int? FindMajority(int[] nums) + { + if (nums?.Length == 0) return null; + + var candidate = FindCandidate(nums!); + return IsMajority(nums!, candidate) ? candidate : null; + } + + private static int FindCandidate(int[] nums) + { + int candidate = nums[0], count = 1; + + for (int i = 1; i < nums.Length; i++) + { + if (count == 0) candidate = nums[i]; + count += nums[i] == candidate ? 1 : -1; + } + + return candidate; + } + + private static bool IsMajority(int[] nums, int candidate) => + nums.Count(n => n == candidate) > nums.Length / 2; +} From ffa07a832bc2bdeba0935f114014f50d5eb47b83 Mon Sep 17 00:00:00 2001 From: 0xsatoshi99 <0xsatoshi99@users.noreply.github.com> Date: Mon, 10 Nov 2025 09:04:07 +0100 Subject: [PATCH 2/3] Fix all 5 Codacy static analysis issues - Add curly braces to single-statement if blocks (lines 16, 28) - Split multi-statement lines into separate lines (lines 16, 28) - Declare variables separately instead of comma-separated (line 24) --- Algorithms/Other/BoyerMooreMajorityVote.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Algorithms/Other/BoyerMooreMajorityVote.cs b/Algorithms/Other/BoyerMooreMajorityVote.cs index 838eb100..bc1925c7 100644 --- a/Algorithms/Other/BoyerMooreMajorityVote.cs +++ b/Algorithms/Other/BoyerMooreMajorityVote.cs @@ -13,7 +13,10 @@ public static class BoyerMooreMajorityVote /// Majority element or null. public static int? FindMajority(int[] nums) { - if (nums?.Length == 0) return null; + if (nums?.Length == 0) + { + return null; + } var candidate = FindCandidate(nums!); return IsMajority(nums!, candidate) ? candidate : null; @@ -21,11 +24,16 @@ public static class BoyerMooreMajorityVote private static int FindCandidate(int[] nums) { - int candidate = nums[0], count = 1; + int candidate = nums[0]; + int count = 1; for (int i = 1; i < nums.Length; i++) { - if (count == 0) candidate = nums[i]; + if (count == 0) + { + candidate = nums[i]; + } + count += nums[i] == candidate ? 1 : -1; } From 94b4f75106e9101d1cdadc423f89e1348c02b487 Mon Sep 17 00:00:00 2001 From: 0xsatoshi99 <0xsatoshi99@users.noreply.github.com> Date: Mon, 10 Nov 2025 10:24:26 +0100 Subject: [PATCH 3/3] Fix null array handling in FindMajority - Change null check from 'nums?.Length == 0' to 'nums == null || nums.Length == 0' - Fixes NullReferenceException when passing null array --- Algorithms/Other/BoyerMooreMajorityVote.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Algorithms/Other/BoyerMooreMajorityVote.cs b/Algorithms/Other/BoyerMooreMajorityVote.cs index bc1925c7..6c3ba77e 100644 --- a/Algorithms/Other/BoyerMooreMajorityVote.cs +++ b/Algorithms/Other/BoyerMooreMajorityVote.cs @@ -13,13 +13,13 @@ public static class BoyerMooreMajorityVote /// Majority element or null. public static int? FindMajority(int[] nums) { - if (nums?.Length == 0) + if (nums == null || nums.Length == 0) { return null; } - var candidate = FindCandidate(nums!); - return IsMajority(nums!, candidate) ? candidate : null; + var candidate = FindCandidate(nums); + return IsMajority(nums, candidate) ? candidate : null; } private static int FindCandidate(int[] nums)