Jump to content

Find Duplicates in an array


Recommended Posts

I have a folder with movies in it.

I read this folder and add the filenames into an array.

Now I want to find the duplicates, I have a function that works but not exactly as needed.

In the folder are for example some names like this

Rambo I

Rambo II

Rambo III

The problem, the result lists me Rambo II as a duplicate, cause it exitsts in the name Rambo III

How to modify the function that only exact the same names are recognized as duplicates and not the ones that are contained in a filename?

Func FindeDuplikate( $aArray )
  Local $oTst = ObjCreate( "Scripting.Dictionary" )
  Local $oRes = ObjCreate( "Scripting.Dictionary" )

  For $i = 0 To UBound( $aArray ) - 1
    If Not $oTst.Exists( $aArray[$i] ) Then
      $oTst( $aArray[$i] ) = 1
    ElseIf Not $oRes.Exists( $aArray[$i] ) Then
      $oRes( $aArray[$i] ) = 1
    EndIf
  Next

  Return $oRes.Keys()
EndFunc

 

Link to comment
Share on other sites

You can query and modify the compare mode of the Scripting.Dictionary as described here.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Scripting.Dictionary will not mix "Rambo II" with "Rambo III".  I believe you got another problem somewhere.  Here a simple way to test it :

#include <Array.au3>

Local $aList = ["Rambo I", "Rambo II", "Rambo III", "RAMBO I", "Rambo ii"]
Local $aDup = FindeDuplikate($aList)
_ArrayDisplay($aDup)

Func FindeDuplikate( $aArray )
  Local $oTst = ObjCreate( "Scripting.Dictionary" )
  Local $oRes = ObjCreate( "Scripting.Dictionary" )
  ;$oTst.CompareMode = 1

  For $i = 0 To UBound( $aArray ) - 1
    If Not $oTst.Exists( $aArray[$i] ) Then
      $oTst( $aArray[$i] ) = 1
    ElseIf Not $oRes.Exists( $aArray[$i] ) Then
      $oRes( $aArray[$i] ) = 1
    EndIf
  Next

  Return $oRes.Keys()
EndFunc

As you can see none is duplicate, but if you uncomment the line of CompareMode, then comparaison will be non-case-sensitive, so last two will be flagged as duplicates.

Link to comment
Share on other sites

Something like this here?

#include <Array.au3>
Global $aFilms[] = ["Rambo", "Rambo II", "Rambo III", "Rocky", "Rocky II", "Rocky III", "Rocky IV", "Possessed", "Edge of Tomorrow", "The Ghost Writer", "rocky ii", "RAMBO iii"]

Global $aDuplicates[UBound($aFilms) * 2], $j = 0
For $i = 0 To UBound($aFilms) - 1
    $aDuplicates[$j] = $aFilms[$i]
    $j += 1
    If Random() < 0.3333333 Then
        $aDuplicates[$j] = $aFilms[$i]
        $j += 1
    EndIf
Next
ReDim $aDuplicates[$j - 1]
;_ArrayShuffle($aDuplicates)
_ArraySort($aDuplicates)
ConsoleWrite("All " & UBound($aDuplicates) & " films:" & @CRLF)
For $i = 0 To UBound($aDuplicates) - 1
    ConsoleWrite($i + 1 & ": " & $aDuplicates[$i] & @CRLF)
Next

ConsoleWrite(@CRLF)
ConsoleWrite("Duplicates:" & @CRLF)
$i = -1
$j = 1
While $i < UBound($aDuplicates) - 2
    $i += 1
    If StringLower($aDuplicates[$i]) == StringLower($aDuplicates[$i + 1]) Then
        ConsoleWrite($j & ". duplicate: " & $aDuplicates[$i] & @CRLF)
        $j += 1
        $i += 1
    EndIf
WEnd

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • 3 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...