I'm working to find the next available number in a set of numbers and have trouble.
My dataset is an array of strings that is then converted to an array of integers. The original dataset may have duplicate numbers.
When I try and find the next available number, the duplicates seem to cause issues yielding bad results
$dataset = "0001","0002","0004","0006","2","5"
#Convert our strings to integers
[array]$Used = foreach($number in $dataset) {
try {
[int]::parse($number)
} catch {
Invoke-Expression -Command $number;
}
}
[array]::sort($Used)
$range = 1..10
$Available = compare $range $Used -PassThru
My results are:
$Dataset =
0001
0002
0004
0006
2
5
$Used =
1
2
2
4
5
6
Noticed that $Used
(which is sorted) shows that 2
is a duplicate:
$Available =
2
3
7
8
9
10
Finally $Available
lists 2
as an available number which is wrong. 2
is actually used twice and the correct answer should be 3
.
Any ideas?