Jump to content
BlazerV60

Search an array for duplicates?

Recommended Posts

BlazerV60

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 this post


Link to post
Share on other sites
somdcomputerguy

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.

Share this post


Link to post
Share on other sites
BlazerV60

Perfect, thanks :D

Share this post


Link to post
Share on other sites
somdcomputerguy

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 this post


Link to post
Share on other sites
iamtheky

#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
  • Like 1

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

Share this post


Link to post
Share on other sites
Chimp

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
  • Like 1

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

Share this post


Link to post
Share on other sites
Chimp
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 this post


Link to post
Share on other sites
Dizzastaffy
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 this post


Link to post
Share on other sites
iamtheky
#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]
      _ArrayAdd($aUniqueUser , "")
      _ArrayConcatenate($aFinal , $aUniqueUser)

Next

_ArrayDisplay($aFinal)

 

  • Like 1

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

Share this post


Link to post
Share on other sites
Dizzastaffy
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]
      _ArrayAdd($aUniqueUser , "")
      _ArrayConcatenate($aFinal , $aUniqueUser)

Next

_ArrayDisplay($aFinal)

 

Perfect thanks very much, works just as I wanted.

Share this post


Link to post
Share on other sites
Dizzastaffy

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 this post


Link to post
Share on other sites
iamtheky

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 this post


Link to post
Share on other sites
Dizzastaffy
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 this post


Link to post
Share on other sites
iamtheky

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

  • Like 1

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

Share this post


Link to post
Share on other sites
Chimp

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
  • Like 1

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

Share this post


Link to post
Share on other sites
iamtheky

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.

  • Like 1

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

Share this post


Link to post
Share on other sites
Dizzastaffy

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 

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Dzenan03
      By Dzenan03
      I want to make a while loop, that creates variables based on a array. For thist I created the array $iDsO with the number and the name of folders in an other folder. Every folder has a different name an I want to create variables(arrays) for each folder that show me all the files in that folder. For example: I have the Folder \Folder1. In it there are the Folders \1, \2, \3. In 1, 2 and 3 there are some files(.png). The array for Folder1 is $iDsO and now I want to crate the arrays $iDsO1, $iDsO2 and $iDsO3 with the files in them can I make something like this:
      While $iDs > 0 ;$iDs is the number of files in Folder1>> $iDsO[0] $iDs#here should come the Foldername for example '1'# = _FileListtoArray(@ProgramFilesDir&"\Folder1\"&$iDsO[$iDs]) $iDs = $iDs - 1 Wend So that in the End I have three variabels ($iDs1, $iDs2 and $iDs3)
       
      Is this posible or if not what could I do instead ( I don´t know the number of folders in Folder1 in the begining).
×