Skip to content

Commit cbb97d7

Browse files
committed
init
1 parent f01d2ab commit cbb97d7

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

.github/workflows/powercli-wf.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ jobs:
1717
uses: actions/checkout@v1
1818
- name: "powershell version"
1919
run: $PSVersionTable
20+
- name: "PowerCLI - silent install"
21+
run: .\scripts\MajorityElement.ps1
2022
- name: "PowerCLI - silent install"
2123
run: .\scripts\Install_PowerCLI.ps1
2224
- name: "PowerCLI - silent install"
23-
run: .\scripts\Install_PowerCLI_Powershellgallery.ps1
25+
run: .\scripts\Install_PowerCLI_Powershellgallery.ps1

scripts/MajorityElement.ps1

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function Find-MajorityElement {
2+
param(
3+
[int[]] $arr
4+
)
5+
6+
$candidate = $arr[0]
7+
$count = 1
8+
9+
# Find the potential majority element
10+
for ($i = 1; $i -lt $arr.Length; $i++) {
11+
if ($arr[$i] -eq $candidate) {
12+
$count++
13+
} else {
14+
$count--
15+
if ($count -eq 0) {
16+
$candidate = $arr[$i]
17+
$count = 1
18+
}
19+
}
20+
}
21+
22+
# Verify candidate is indeed the majority element
23+
$count = 0
24+
foreach ($num in $arr) {
25+
if ($num -eq $candidate) {
26+
$count++
27+
}
28+
}
29+
30+
# Determine if the candidate is the majority element
31+
if ($count -gt [math]::floor($arr.Length / 2)) {
32+
return $candidate
33+
} else {
34+
return -1 # -1 indicates no majority element
35+
}
36+
}
37+
38+
# Example usage
39+
$arr1 = @(2, 2, 1, 1, 1, 2, 2)
40+
$majorityElement1 = Find-MajorityElement $arr1
41+
if ($majorityElement1 -ne -1) {
42+
Write-Output "Majority Element: $($majorityElement1)"
43+
} else {
44+
Write-Output "No Majority Element Found"
45+
}
46+
47+
$arr2 = @(3, 4, 2, 4, 2, 4, 4)
48+
$majorityElement2 = Find-MajorityElement $arr2
49+
if ($majorityElement2 -ne -1) {
50+
Write-Output "Majority Element: $($majorityElement2)"
51+
} else {
52+
Write-Output "No Majority Element Found"
53+
}

0 commit comments

Comments
 (0)