Skip to content

Commit 7353537

Browse files
committed
init
1 parent cbb97d7 commit 7353537

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

.github/workflows/powercli-wf.yml

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ jobs:
1717
uses: actions/checkout@v1
1818
- name: "powershell version"
1919
run: $PSVersionTable
20-
- name: "PowerCLI - silent install"
21-
run: .\scripts\MajorityElement.ps1
22-
- name: "PowerCLI - silent install"
23-
run: .\scripts\Install_PowerCLI.ps1
24-
- name: "PowerCLI - silent install"
25-
run: .\scripts\Install_PowerCLI_Powershellgallery.ps1
20+
- name: "PowerCLI - MajorityElement"
21+
run: .\scripts\MajorityElement.ps1
22+
- name: "PowerCLI - MajorityElement with modules"
23+
run: .\scripts\MajorityElement_module.ps1
24+
# - name: "PowerCLI - silent install"
25+
# run: .\scripts\Install_PowerCLI.ps1
26+
# - name: "PowerCLI - silent install"
27+
# run: .\scripts\Install_PowerCLI_Powershellgallery.ps1

scripts/MajorityElement.psm1

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Module script MajorityElement.psm1
2+
3+
function Find-MajorityElement {
4+
param(
5+
[int[]] $arr
6+
)
7+
8+
$candidate = $arr[0]
9+
$count = 1
10+
11+
# Find the potential majority element
12+
for ($i = 1; $i -lt $arr.Length; $i++) {
13+
if ($arr[$i] -eq $candidate) {
14+
$count++
15+
} else {
16+
$count--
17+
if ($count -eq 0) {
18+
$candidate = $arr[$i]
19+
$count = 1
20+
}
21+
}
22+
}
23+
24+
# Verify candidate is indeed the majority element
25+
$count = 0
26+
foreach ($num in $arr) {
27+
if ($num -eq $candidate) {
28+
$count++
29+
}
30+
}
31+
32+
# Determine if the candidate is the majority element
33+
if ($count -gt [math]::floor($arr.Length / 2)) {
34+
return $candidate
35+
} else {
36+
return -1 # -1 indicates no majority element
37+
}
38+
}
39+
40+
Export-ModuleMember -Function Find-MajorityElement

scripts/MajorityElement_module.ps1

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Import the module
2+
Import-Module -Name .\MajorityElement.psm1
3+
4+
# Example arrays
5+
$arr1 = @(2, 2, 1, 1, 1, 2, 2)
6+
$arr2 = @(3, 4, 2, 4, 2, 4, 4)
7+
8+
# Find majority element and display result
9+
$majorityElement1 = Find-MajorityElement -arr $arr1
10+
if ($majorityElement1 -ne -1) {
11+
Write-Output "Majority Element: $($majorityElement1)"
12+
} else {
13+
Write-Output "No Majority Element Found"
14+
}
15+
16+
$majorityElement2 = Find-MajorityElement -arr $arr2
17+
if ($majorityElement2 -ne -1) {
18+
Write-Output "Majority Element: $($majorityElement2)"
19+
} else {
20+
Write-Output "No Majority Element Found"
21+
}

0 commit comments

Comments
 (0)