# Search an array for duplicates?

## Recommended Posts

Hello everyone ,

Let's say I have an array that contains these numbers: 2,5,7,2,25,6,31,1,24,5,7,8,7,99

How would I make my program be able to tell me something like "There are three 7's".

Is it possible to make an array detect duplicates and notify you?

Thanks,

Brian

##### Share on other sites

The _ArrayFindAll() UDF can be used to do this for you.

edit:

Here's an example:

```#include <Array.au3>

Local \$aArray[14] = [2,5,7,2,25,6,31,1,24,5,7,8,7,99]
Local \$aResult = _ArrayFindAll(\$aArray, 7)
ConsoleWrite("There are " & UBound(\$aResult) & " 7's"& @LF)```
Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Perfect, thanks

##### Share on other sites

You bet. Good luck with your project.

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

##### Share on other sites

```#Include <Array.au3>

Local \$aArray[14] = [2,5,7,2,25,6,31,1,24,5,7,8,7,99]

For \$i = ubound(\$aArray) - 1 to 0 step -1
Local \$aResult = _ArrayFindAll(\$aArray, \$aArray[\$i])
if ubound(\$aResult) > 1 Then
msgbox (0 , '' , "There are " & ubound(\$aResult) & ":  " & \$aArray[\$i])
for \$x = UBound(\$aResult) - 1 to 0 step - 1
_ArrayDelete(\$aArray , \$aResult[\$x])
Next
Endif
\$i = \$i - (UBound(\$aResult) - 1)
Next```

Edited by boththose

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

a little improvement
this searches any duplicate (no need to know prior what to search)
note: if there are more empty elements are considered duplicate

```#include <Array.au3>

Local \$aArray[14] = [2,5,7,2,25,6,31,1,24,5,7,8,7,99]
Local \$aResult
Local \$aArrayUnique = _ArrayUnique(\$aArray, 1, 0, 0, 0) ; keep only single elements
If UBound(\$aArrayUnique) = UBound(\$aArray) Then
ConsoleWrite("There are not duplicates." & @CRLF)
Else
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$aArray, \$aArrayUnique[\$i])
If UBound(\$aResult) > 1 Then
ConsoleWrite("There are " & UBound(\$aResult) & " '" & \$aArrayUnique[\$i] & "'" & @LF)
EndIf
Next
EndIf```

edit:

I saw the post by boththose only after I posted this
Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Oups, sorry, it's not the good post...

@Chimp, your code does not work (seems to be an error with _ArrayUnique

Edited by jguinch
Posted you the bad topic, sorry

##### Share on other sites
7 minutes ago, jguinch said:

Oups, sorry, it's not the good post...

@Chimp, your code does not work (seems to be an error with _ArrayUnique

Yes, cause of the second parameter of _ArrayUnique() that now is 0 based, while in previous versions it was 1 based.

Updated working code:

```#include <Array.au3>

Local \$aArray[14] = [2,5,7,2,25,6,31,1,24,5,7,8,7,99]
Local \$aResult
Local \$aArrayUnique = _ArrayUnique(\$aArray, 0, 0, 0, 0) ; keep only single elements
_ArrayDisplay(\$aArrayUnique)
If UBound(\$aArrayUnique) = UBound(\$aArray) Then
ConsoleWrite("There are not duplicates." & @CRLF)
Else
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$aArray, \$aArrayUnique[\$i])
If UBound(\$aResult) > 1 Then
ConsoleWrite("There are " & UBound(\$aResult) & " '" & \$aArrayUnique[\$i] & "'" & @LF)
EndIf
Next
EndIf```

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites
On 3/26/2016 at 8:39 PM, Chimp said:

Yes, cause of the second parameter of _ArrayUnique() that now is 0 based, while in previous versions it was 1 based.

Updated working code:

```#include <Array.au3>

Local \$aArray[14] = [2,5,7,2,25,6,31,1,24,5,7,8,7,99]
Local \$aResult
Local \$aArrayUnique = _ArrayUnique(\$aArray, 0, 0, 0, 0) ; keep only single elements
_ArrayDisplay(\$aArrayUnique)
If UBound(\$aArrayUnique) = UBound(\$aArray) Then
ConsoleWrite("There are not duplicates." & @CRLF)
Else
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$aArray, \$aArrayUnique[\$i])
If UBound(\$aResult) > 1 Then
ConsoleWrite("There are " & UBound(\$aResult) & " '" & \$aArrayUnique[\$i] & "'" & @LF)
EndIf
Next
EndIf```

Hi sorry to open an older thread, it my first post and stuck. @Chimp I am using the above which is close to working for me.

What I have is an array with one column that has usernames and the other column passwords. What I want this script to do is list usernames who share the same password, so to count the column2 and display it. I can get the above working if it is on column1 it displays and counts usernames that are duplicates fine. If I change it to do this on column 2 it detects there is duplicates, but I cant get it to count or display them. I am sure if is something simple,but I just cant get this working and would really appreciate some help.

Here is an example of my data that is split into 2 columns in an array

```user1: 76560BB696114467
user2: 76560BB696114467
user3: 76560BB696114467
user4: 76560BB696114467
user5: 76560BB696114467
user6: 765454545454545
user7: 7666565656565656
user8: 76560BB6uyuyu567
user1: 76560BB696114467```

The array where this data sits is called avIPS.

```Local \$aResult
Local \$aArrayUnique = _ArrayUnique(\$avIPs, 0, 0, 0, 0) ; keep only single elements
;_ArrayDisplay(\$aArrayUnique)

If UBound(\$aArrayUnique) = UBound(\$avIPs) Then
ConsoleWrite("There are not duplicates." & @CRLF)

Else
ConsoleWrite("There are duplicates." & @CRLF)
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$avIPs, \$aArrayUnique[\$i])
If UBound(\$aResult) > 1 Then
ConsoleWrite("There are " & UBound(\$aResult) & " '" & \$aArrayUnique[\$i] & "'" & @LF)
EndIf
Next
EndIf```

So running the above on my array on matches column1 fine and says there are 2 user1 which is correct

```There are duplicates.
There are 2 'user1'```

If I alter the above to try and work on the password column (changing 0,0,0,0 to 1,0,0,0 ) then it detects there are duplicates but doesnt count or display them, so is something I need to edit around the Ubound I think?

```Local \$aResult
Local \$aArrayUnique = _ArrayUnique(\$avIPs, 1, 0, 0, 0) ; keep only single elements
;_ArrayDisplay(\$aArrayUnique)

If UBound(\$aArrayUnique) = UBound(\$avIPs) Then
ConsoleWrite("There are not duplicates." & @CRLF)

Else
ConsoleWrite("There are duplicates." & @CRLF)
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$avIPs, \$aArrayUnique[\$i])
;If UBound(\$aResult) > 1 Then
ConsoleWrite("There are " & UBound(\$aResult) & " '" & \$aArrayUnique[\$i] & "'" & @LF)
; EndIf
Next
EndIf```

This is then my output, so it knows there are duplicates, but wont count them.

```There are duplicates.
There are 1 ''
There are 0 ' 76560BB696114467'
There are 0 ' 765454545454545'
There are 0 ' 7666565656565656'
There are 0 ' 76560BB6uyuyu567'```

Worst case I would like it to say there are 5x users with password 76560BB696114467 or even better the following usernames have the same password of 76560BB696114467.

This are not real passwords, just an example.

Any pointers I would be really grateful. thanks

##### Share on other sites
```#include <Array.au3>
local \$aFinal[0]

Local \$avArray[9][2] = [ _
["user1", "76560BB696114467"], _
["user2", "76560BB696114467"], _
["user3", "76560BB696114467"], _
["user4", "76560BB696114467"], _
["user5", "76560BB696114467"], _
["user6", "765454545454545"], _
["user7", "7666565656565656"], _
["user8", "76560BB6uyuyu567"], _
["user1", "76560BB696114467"]]

\$aUniquePW = _ArrayUnique(\$avArray , 1)

For \$i = 1 to ubound(\$aUniquePW) - 1

\$aMatch = _ArrayFindAll(\$avArray , \$aUniquePW[\$i] , 0 , 0 , 0 , 0 , 1)

for \$k = 0 to ubound(\$aMatch) - 1
\$aMatch[\$k] = \$avArray[\$aMatch[\$k]][0]
next

\$aUniqueUser = _ArrayUnique(\$aMatch)
\$aUniqueUser[0] = \$aUniquePW[\$i]
_ArrayConcatenate(\$aFinal , \$aUniqueUser)

Next

_ArrayDisplay(\$aFinal)```

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites
16 minutes ago, iamtheky said:
```#include <Array.au3>
local \$aFinal[0]

Local \$avArray[9][2] = [ _
["user1", "76560BB696114467"], _
["user2", "76560BB696114467"], _
["user3", "76560BB696114467"], _
["user4", "76560BB696114467"], _
["user5", "76560BB696114467"], _
["user6", "765454545454545"], _
["user7", "7666565656565656"], _
["user8", "76560BB6uyuyu567"], _
["user1", "76560BB696114467"]]

\$aUniquePW = _ArrayUnique(\$avArray , 1)

For \$i = 1 to ubound(\$aUniquePW) - 1

\$aMatch = _ArrayFindAll(\$avArray , \$aUniquePW[\$i] , 0 , 0 , 0 , 0 , 1)

for \$k = 0 to ubound(\$aMatch) - 1
\$aMatch[\$k] = \$avArray[\$aMatch[\$k]][0]
next

\$aUniqueUser = _ArrayUnique(\$aMatch)
\$aUniqueUser[0] = \$aUniquePW[\$i]
_ArrayConcatenate(\$aFinal , \$aUniqueUser)

Next

_ArrayDisplay(\$aFinal)```

Perfect thanks very much, works just as I wanted.

##### Share on other sites

Sorry to be a pain @iamtheky what would the easiest way using your example to only show usernames/passwords where greater than 1? i.e only display the items in the array if more than 1 user shares the same password. Basically I am only interested in if users have the same password as others.

many thanks

Edited by Dizzastaffy

##### Share on other sites

check \$aUniqueUser[0] > 1

Do that right after its created, and If true then proceed on to change the value in element 0 to the password, add the blank row, and concatenate with final, else continueloop.

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites
18 minutes ago, iamtheky said:

check \$aUniqueUser[0] > 1

Do that right after its created, and If true then proceed on to change the value in element 0 to the password, add the blank row, and concatenate with final, else continueloop.

Thanks. So if I understand correct you mean add a if \$aUniqueUser[0] > 1 then after this line

or do you mean after the end of the script and array afinal is created?

`    \$aUniqueUser = _ArrayUnique(\$aMatch)`

##### Share on other sites

yeah, you can put an _ArrayDisplay(\$aUniqueUser) line there as well to debug if there are issues or edge cases unaccounted for.

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

You can also view users that share passwords groupped in a treeview...

```#include <Array.au3>
#include <TreeViewConstants.au3>
#include <GUITreeView.au3>
#include <WindowsConstants.au3>

Local \$aArray = [ _
["user1", "76560BB696114467"], _
["user2", "76560BB696114467"], _
["user3", "76560BB696114467"], _
["user4", "76560BB696114467"], _
["user5", "76560BB696114467"], _
["user6", "76560BB6uyuyu567"], _
["user7", "765454545454545"], _
["user8", "76560BB6uyuyu567"], _
["user9", "765454545454545"]]

Local \$aResult, \$aArrayUnique = _ArrayUnique(\$aArray, 1, 0, 1, 0) ; keep only single elements
If UBound(\$aArrayUnique) = UBound(\$aArray) Then
MsgBox(0, "Info", "There are not duplicates.")
Else
\$hMain = GUICreate("unwary users", 280, 400, 10, 10)
\$hTree = GUICtrlCreateTreeView(5, 5, 270, 390, BitOR(\$TVS_HASBUTTONS, \$TVS_HASLINES, \$TVS_LINESATROOT, \$TVS_DISABLEDRAGDROP, \$TVS_SHOWSELALWAYS), \$WS_EX_CLIENTEDGE)
GUISetState()
_GUICtrlTreeView_BeginUpdate(\$hTree)
Local \$hAncestor = _GUICtrlTreeView_Add(\$hTree, 0, "Shared pwds")
For \$i = 0 To UBound(\$aArrayUnique) - 1
\$aResult = _ArrayFindAll(\$aArray, \$aArrayUnique[\$i], 0, 0, 0, 0, 1) ; password
If UBound(\$aResult) > 1 Then
Local \$hGroup = _GUICtrlTreeView_AddChild(\$hTree, \$hAncestor, \$aArray[\$aResult[0]][1])
For \$i1 = 0 To UBound(\$aResult) - 1
_GUICtrlTreeView_AddChild(\$hTree, \$hGroup, \$aArray[\$aResult[\$i1]][0]) ; Users
Next
EndIf
Next
_GUICtrlTreeView_EndUpdate(\$hTree)
MsgBox(0, "Pause", "Click OK to end")
EndIf```

Edited by Chimp
debugged listing

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

There it is new folk.  Give all the details, show effort, get multiple responses that solve the problem. That's how this biatch works.

No one even harassed him for resjacking (which i just coined and should be used everywhere) a thread, because its related, and a complete question.

```,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)```

##### Share on other sites

Thanks all. There has been many times I wanted to post, but got there in the end by looking, trying, more trying and looking at other answers on the forum. I have gone from never using to writing something that is very useful. I didn't want to just post hi how do I do this without even trying, I tried but was above my knowledge in this case, I have several arrays working by reading files, splitting data, etc, but this one got me. thanks

## 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

• ### Similar Content

• By sksbir
Hi
Trying this from autoit v3.3.14.5 and SCITE 3.19.102.1901.0 :
help file , page "Language Reference - Variables" , sample autoit script about maps:

Maps must be declared before use by defining their scope using the 'Global/Local/Static' keywords. Local \$mControls[]
So is my test script : only with this local declation.
-check syntax is OK
- running script :

test.au3" (13) : ==> Variable subscript badly formatted.: Local \$mControls[] Local \$mControls[^ ERROR ->14:51:49 AutoIt3.exe ended.rc:1
any clue ?
• By Blitzkid
Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
to smth. like

Here is my Code
#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> \$filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local \$arr[3] = ["music", "picture", "video"] For \$i = 0 To UBound(\$arr) - 1 Local \$arrayfiles = _FileListToArray(\$filedir & \$arr[\$i], \$arr[\$i] & "*", 1) If @error = 1 Then ConsoleWrite(\$arr[\$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite(\$arr[\$i] & "Error 2") ;Exit EndIf \$arrayfilter = _ArrayMax(\$arrayfiles, 0, 1) Global \$stringfiles = StringReplace(\$arrayfilter, \$arr[\$i], "") ConsoleWrite(\$arrayfilter & @CRLF) Next EndFunc ;==>_checkfile
• By jmp
Script running good but error in line 7.
When i run this script :
#include <IE.au3> #include <Array.au3> \$oIE = _IEAttach ("Shop") \$oTable = _IETableGetCollection (\$oIE, 1) \$aTableData = _IETableWriteToArray (\$oTable) For \$inumber = 1 To UBound(\$aTableData) -1 \$table = \$aTableData[4][\$inumber] MsgBox(0, "", \$table) Next I got Error: array variable has incorrect number of subscripts or subscript dimension range exceeded

• Good morning guys, i hope that you're all well.
guys, i have a problem and i hope that you can help me
i've created an 3d array
the array Contain a Categories info
as folow
\$array[n][0][0] = Categorie name
\$array[n][0][1] = Categorie file path
\$array[n][0][2] = Categorie contents number
\$array[n][m][0] = link name
\$array[n][m][1] = link url
\$array[n][m][2] = link section name
in my tool i want to add an option to delete a Category
as you know the Categorie mean that must delete a region from the array
when i tried to use _arrayDelete
with the 2d array it work well
but here i couldn't find any way to do that, can any one help me please?

i tried to create a function that read a folder files to 3d array
e.g
\$array[n][0][0] = ctName
\$array[n][0][1] = ctFilePath
\$array[n][0][2] = crtsections number
\$array[n][m][0] = KeyName
\$array[n][m][1] = KeyVal
\$array[n][m][2] = keySectionName

that the array
when i put one file into the folder all things work fine
but when i put more than one file
the last file worked fine but the others only the first key is showing
please can you help me to correct this problem
here is the example with the folder
please accept my greetings
and thanks in advance

array3d.zip
×

• Wiki

• Back

• #### Beta

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