# Need a way to compare 3 arrays to find if two of them have a same value. (My brain is going to explode, yes)

## Recommended Posts

So, basically I have three arrays at a given time. Let's say they are \$x[], \$y[] and\$z[]

For example if \$x has values 1, 2, 3, \$y has 5, 6, 7 and \$z has 3, 4, 5 then I need the script to give me values 3 and 5 because they are in two arrays at the same time. I thought of using binarysearch to find if they are in any of the arrays one at a time (I need to check for numbers 1-9 only) but I had no idea where to store what and what the hell I'm doing at all. It just gets too complicated.

Is there maybe a simplier way to achive what I'm trying to?

Ideally in the end I want the script to return a single number from 1-9 if it's at least in two of the arrays and it's the only number that's in two arrays at the same time and maybe a 0 if none of the numbers are in two arrays at the same time or there is more then one number that's in two arrays.

##### Share on other sites

So, basically I have three arrays at a given time. Let's say they are \$x[], \$y[] and\$z[]

Depends on how many possible solutions you could have, or if there will only be one solution.

A big portion of your complication is you don't seem to be able to articulate exactly what you want. If you can't describe it clearly, it's really hard to code it.

If you can only have one solution, then:

build an array with the number of times you found a 1-9.

iterate through each array, adding 1 to the appropriate element as you find it.

test the new value after its addition - if it equals two , you're done.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

##### Share on other sites

For clarification then, I'm making a sudoku solver. For each of the 81 squares (that have a value from 1-9, 0 if blank) I have three arrays. 9x1 cell that goes from left to right. 1x9 cell that goes from top to bottom and a 3x3 cell that goes left to right and then top to bottom. So for each square I need to see if there is only one possible value for it, if there is, the value of the square will be changed into that.

##### Share on other sites

For clarification then, I'm making a sudoku solver. For each of the 81 squares (that have a value from 1-9, 0 if blank) I have three arrays. 9x1 cell that goes from left to right. 1x9 cell that goes from top to bottom and a 3x3 cell that goes left to right and then top to bottom. So for each square I need to see if there is only one possible value for it, if there is, the value of the square will be changed into that.

I think you're going to need to rethink your approach. What you need is a mechanism to determine which elements you should be testing. Most sudoku solver approaches I have seen involve starting each square w/ all possible options, and then eliminating the duplicates...

In your case 0 = 1-9, which is fine, but when you eliminate an option, how are you going to store the fact that a given cell can be 1-5,7,9 if you only populate solved cells?

Much easier, IMO to start your "blank" cells with the string "123456789", and then eliminate based on the sudoku ruleset w/ a StringReplace() . When StringLen(\$array[\$x][\$y]) = 1 then you have solved that cell.

This way , you have one 9x9 array, and you develop the functions for calling a given row/line (trivial) and each of the 9 subsquares (either do the math, or create a squares array that contains the xy coords of each cell in the square).

Of course, this is only one approach, and relying on the String... functions will cost speed.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

##### Share on other sites

I think you're going to need to rethink your approach. What you need is a mechanism to determine which elements you should be testing. Most sudoku solver approaches I have seen involve starting each square w/ all possible options, and then eliminating the duplicates...

In your case 0 = 1-9, which is fine, but when you eliminate an option, how are you going to store the fact that a given cell can be 1-5,7,9 if you only populate solved cells?

Much easier, IMO to start your "blank" cells with the string "123456789", and then eliminate based on the sudoku ruleset w/ a StringReplace() . When StringLen(\$array[\$x][\$y]) = 1 then you have solved that cell.

This way , you have one 9x9 array, and you develop the functions for calling a given row/line (trivial) and each of the 9 subsquares (either do the math, or create a squares array that contains the xy coords of each cell in the square).

Of course, this is only one approach, and relying on the String... functions will cost speed.

I may be missing something, but this alternate solution will still not help me with the current problem. How do I know that the current square can be declared solved?

##### Share on other sites

What is the rule that defines a current square as being solved? Test for that.

##### Share on other sites

What is the rule that defines a current square as being solved? Test for that.

Well, that's what the three arrays are for and that's why I have a problem : ).

##### Share on other sites

Ideally in the end I want the script to

Test 1: return a single number from 1-9 if it's at least in two of the arrays

and

Test 2: it's the only number that's in two arrays at the same time

and

Test 3: maybe a 0 if none of the numbers are in two arrays at the same time

or

Test 4: there is more then one number that's in two arrays.

Is this what you want to test for? You just want a series of nested loops, don't you? Edited by Confuzzled

##### Share on other sites

Is this what you want to test for? You just want a series of nested loops, don't you?

Probably so, It's the first test I'm having a problem with.

##### Share on other sites

Need to re-create what's been done a few times?

If not:

http://www.autoitscript.com/forum/index.ph...st&p=171580

just search for sudoku

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

Need to re-create what's been done a few times?

Actually yes.

It's more of a learning-making yourself thing then actually needing the program.

##### Share on other sites

I think this is what you were asking in the original post:

```#include <Array.au3>

Dim \$a1[3] = [1, 2, 3], \$a2[5] = [5, 6, 7, 8, 9], \$a3[2] = [3, 5]
MsgBox(0, "Dupes?", _HasDupe(\$a1, \$a2, \$a3))

Func _HasDupe(\$a_array01, \$a_array02, \$a_array03, \$i_ReturnType = 0)
Local \$Dupes[1] = [0]
Local \$found = False
For \$i = 0 To UBound(\$a_array01) - 1
For \$j = 0 To UBound(\$a_array02) - 1
If \$a_array01[\$i] = \$a_array02[\$j] Then
\$found = False
For \$x = 1 To \$Dupes[0]
If \$Dupes[\$x] = \$a_array01[\$i] Then
\$found = True
ExitLoop
EndIf
Next
If Not \$found Then
\$Dupes[0] += 1
ReDim \$Dupes[\$Dupes[0] + 1]
\$Dupes[\$Dupes[0]] = \$a_array01[\$x]
EndIf
EndIf
For \$k = 0 To UBound(\$a_array03) - 1
If \$a_array01[\$i] = \$a_array03[\$k]Then
\$found = False
For \$x = 1 To \$Dupes[0]
If \$Dupes[\$x] = \$a_array01[\$i] Then
\$found = True
ExitLoop
EndIf
Next
If Not \$found Then
\$Dupes[0] += 1
ReDim \$Dupes[\$Dupes[0] + 1]
\$Dupes[\$Dupes[0]] = \$a_array01[\$i]
EndIf
EndIf
If \$a_array02[\$j] = \$a_array03[\$k]Then
\$found = False
For \$x = 1 To \$Dupes[0]
If \$Dupes[\$x] = \$a_array02[\$j] Then
\$found = True
ExitLoop
EndIf
Next
If Not \$found Then
\$Dupes[0] += 1
ReDim \$Dupes[\$Dupes[0] + 1]
\$Dupes[\$Dupes[0]] = \$a_array02[\$j]
EndIf
EndIf
Next
Next
Next
If \$Dupes[0] = 0 Then Return ''
If Not \$i_ReturnType Then
Local \$s_return = ''
For \$x = 1 To \$Dupes[0]
\$s_return &= \$Dupes[\$x] & ','
Next
Return StringTrimRight(\$s_return, 1)
Else
Return \$Dupes
EndIf

EndFunc   ;==>_HasDupe```
Edited by gafrost

Don't argue with an idiot; people watching may not be able to tell the difference.

##### Share on other sites

I think this is what you were asking in the original post:

I think it is, thanks.

Edited by Yogiz

##### Share on other sites

I think it is, thanks.

Just playing around, but this will let you use any number of arrays:

```#include <Array.au3>

Dim \$a1[3] = [1, 2, 3], \$a2[5] = [5, 6, 7, 8, 9], \$a3[2] = [3, 5]
Dim \$a_a[4] = [3, \$a1, \$a2, \$a3]
MsgBox(0, "Dupes?", _SearchForDupes(\$a_a))

Func _SearchForDupes(\$a_arrays, \$i_ReturnType = 0)
Local \$Dupes[1] = [0]
If \$a_arrays[0] < 2 Then Return ''
For \$i = 1 To \$a_a[0] - 1
For \$j = \$i + 1 To \$a_a[0]
_FindDupe(\$a_a[\$i], \$a_a[\$j], \$Dupes)
Next
Next
If \$Dupes[0] = 0 Then Return ''
_ArraySort(\$Dupes)
If Not \$i_ReturnType Then
Local \$s_return = ''
For \$x = 1 To \$Dupes[0]
\$s_return &= \$Dupes[\$x] & ','
Next
Return StringTrimRight(\$s_return, 1)
Else
Return \$Dupes
EndIf

EndFunc   ;==>_SearchForDupes

Func _FindDupe(ByRef \$a_array01, ByRef \$a_array02, ByRef \$Dupes)
Local \$found = False
For \$i = 0 To UBound(\$a_array01) - 1
For \$j = 0 To UBound(\$a_array02) - 1
If \$a_array01[\$i] = \$a_array02[\$j] Then
\$found = False
For \$x = 1 To \$Dupes[0]
If \$Dupes[\$x] = \$a_array01[\$i] Then
\$found = True
ExitLoop
EndIf
Next
If Not \$found Then
\$Dupes[0] += 1
ReDim \$Dupes[\$Dupes[0] + 1]
\$Dupes[\$Dupes[0]] = \$a_array01[\$i]
EndIf
EndIf
Next
Next
EndFunc   ;==>_FindDupe```
Edited by gafrost

Don't argue with an idiot; people watching may not be able to tell the difference.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

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