# compare array of numbers

## Recommended Posts

i have an array of numbers (always 9)

could be ....

0,0,5,1,0,9,2,0,0

so... basically i want to find all the missing numbers!

in this case the missing numbers are....

3,4,6,7 and 8

any cool ideas

##### Share on other sites

i have an array of numbers (always 9)

could be ....

0,0,5,1,0,9,2,0,0

so... basically i want to find all the missing numbers!

in this case the missing numbers are....

3,4,6,7 and 8

any cool ideas

<{POST_SNAPBACK}>

if the numbers will always be single digit, you don't even need an array, you could just use a for loops with StringInStr to check each number if it's there. but here's a pretty simple way...

```#include <array.au3>
dim \$Numbers[9]
\$numbers = StringSplit(InputBox("paste in number string","paste in number string"),",")
\$notin = ""
for \$i = 0 to 9; check if number exists
\$in = 0
for \$i2 = 0 to 8; check each element
if \$numbers[\$i2] = \$i then \$in = 1
Next
if \$in = 0 then
if StringLen(\$notin) < 1 then
\$notin = \$i
Else
\$notin = \$notin & "," & \$i
EndIf
EndIf
Next
MsgBox(0,"Not in","These numbers were not in the array" & @crlf & \$notin)```

##### Share on other sites

Just for the fun of it (and to learn a bit) I compiled the code that you offered and then decompiled it. I wondered if array.au3 would be included in the compiled version even though it is not used in the rest of the script - it was.

Never mind me - just answering questions that no one is asking...atm

[size="1"][font="Arial"].[u].[/u][/font][/size]

##### Share on other sites

i have an array of numbers (always 9)

could be ....

0,0,5,1,0,9,2,0,0

so... basically i want to find all the missing numbers!

in this case the missing numbers are....

3,4,6,7 and 8

any cool ideas

<{POST_SNAPBACK}>

here's an idea:

```Func FindMissing(\$aIn, \$iBase)
If \$iBase Then
\$iBase = 1
Local \$Max =\$aIn[0]
Else
\$iBase = 0
\$Max = UBound(\$aIn) - 1
EndIf
_ArraySort(\$aIn, 0, 1)
Local \$aOut[\$aIn[\$Max]], \$i, \$j
For \$i = \$iBase to \$Max - 1
If \$aIn[\$i] + 1 <> \$aIn[\$i + 1] Then
For \$j = \$aIn[\$i] + 1 to \$aIn[\$i + 1] - 1
\$aOut[0] = \$aOut[0] + 1
\$aOut[\$aOut[0]] = \$j
Next
EndIf
Next
ReDim \$aOut[\$aOut[0] + 1]
Return \$aOut
EndFunc```

My UDF Threads:Other UDF Threads I Participated:Base64 Conversions

##### Share on other sites

Lookin pretty good! and i understand it!

thanks

blindwig, it looks pretty impressive! i'm still working my way thru the code.

many thanks

##### Share on other sites

```Func FindMissing(\$aIn, \$iBase)
;setup for 0-based or 1-based arrays
If \$iBase Then
\$iBase = 1
Local \$Max =\$aIn[0]
Else
\$iBase = 0
\$Max = UBound(\$aIn) - 1
EndIf
;sort the array
_ArraySort(\$aIn, 0, \$iBase)
;create an output array that will contain the list of missing numbers
Local \$aOut[\$aIn[\$Max] - \$aIn[\$iBase] + 2], \$i, \$j
;Loop through the input array
For \$i = \$iBase to \$Max - 1
;check that each number is 1 less than the next number
If \$aIn[\$i] + 1 <> \$aIn[\$i + 1] Then
;loop through the missing numbers
For \$j = \$aIn[\$i] + 1 to \$aIn[\$i + 1] - 1
;increase the size of the list of missing numbers
\$aOut[0] = \$aOut[0] + 1
;add this number to the list of missing numbers
\$aOut[\$aOut[0]] = \$j
Next
EndIf
Next
;trim the size of the list of missing numbers to actual size
ReDim \$aOut[\$aOut[0] + 1]
;return the list of missing numbers
Return \$aOut
EndFunc```

Edit: Fixed some code bugs handling 0- vs 1-based arrays

Edited by blindwig

My UDF Threads:Other UDF Threads I Participated:Base64 Conversions

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...