# Search an array for duplicates?

Go to solution Solved by somdcomputerguy,

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

• Solution

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

Chimp

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

##### Share on other sites

• 1 year later...

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

Chimp

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

##### Share on other sites

• 3 months later...
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

Chimp

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

• ### Recently Browsing   0 members

• No registered users viewing this page.
• ### Similar Content

By Zedna,

• 95 views
• #### Help with delete row from array if value exist in cell

By kemo1987,

• 4 replies
• 374 views
• #### delete rows from array if just 1 empty cell exist

By kemo1987,

• 3 replies
• 310 views
• #### Count the number of times the highest number is repeated.

By Dsmoeg999,

• 8 replies
• 430 views
• #### Using an array inside of an array to transfer lots of files.

By TheAlienDoctor,

• 582 views
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...