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

    • lattey
      By lattey
      hi,
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
       
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
       
      What is the best approach to accomplishing this method?
             
    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • 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.
       
×