Skip to content

Commit 94b4f75

Browse files
committed
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
1 parent ffa07a8 commit 94b4f75

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Algorithms/Other/BoyerMooreMajorityVote.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public static class BoyerMooreMajorityVote
1313
/// <returns>Majority element or null.</returns>
1414
public static int? FindMajority(int[] nums)
1515
{
16-
if (nums?.Length == 0)
16+
if (nums == null || nums.Length == 0)
1717
{
1818
return null;
1919
}
2020

21-
var candidate = FindCandidate(nums!);
22-
return IsMajority(nums!, candidate) ? candidate : null;
21+
var candidate = FindCandidate(nums);
22+
return IsMajority(nums, candidate) ? candidate : null;
2323
}
2424

2525
private static int FindCandidate(int[] nums)

0 commit comments

Comments
 (0)