# Get the number of occurrences for each number
$numberCount = $NumberArray | Group-Object | Sort-Object -Descending count
# check that it is possble to calculate a mode
if(@($numberCount | Select-Object Count -Unique).Count -gt 1){
# Get the count for the numbers with the most occurrences
$topCount = ($numberCount | Select-Object -First 1).Count
# Get the most frequently occurring values
$modevalue = ($numberCount | Where-Object{$_.Count -eq $topCount}).Name
}
$modevalue
Use this snippet to compute the mode from an array of numerical values
PS C:\> $NumberArray = 10,23,24,43,118,23 >> $numberCount = $NumberArray | Group-Object | Sort-Object -Descending count >> if(@($numberCount | Select-Object Count -Unique).Count -gt 1){ >> # Get the count for the numbers with the most occurrences >> $topCount = ($numberCount | Select-Object -First 1).Count >> # Get the most frequently occurring values >> $modevalue = ($numberCount | Where-Object{$_.Count -eq $topCount}).Name >> } >> $modevalue 23