Dgameman1

How to delete duplicates of in an Array?

11 posts in this topic

I know I can use _ArrayUnique to give back the Unique arrays, but I want to actually delete both of the duplicates if there's more than one at all.

Local $Usernames[3] = ["Dan", "Bob", "Billy"]
Local $blackListUsernames[2] = ["Dan", "Scott"]
_ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1.
$Usernames = _ArrayUnique($Usernames) ;Uniques the array.

This issue with this, is that $Usernames will contain Dan, Bob, Billy, Scott.

 

I can't seem to find a way to make it so that if there are any duplicates, it'll just remove both of them, for example, I want $Usernames to have just Bob, Billy, Scott.

There will only be either two or one of the same value. Never more than 2.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do it like this:

#include <Array.au3>

Local $Usernames[3] = ["Dan", "Bob", "Billy"]
Local $blackListUsernames[2] = ["Dan", "Scott"]
For $i=UBound($Usernames)-1 To 0 Step -1
    $iFound=_ArraySearch($blackListUsernames,$Usernames[$i])
    If Not @error Then
        _ArrayDelete($Usernames,$i)
        _ArrayDelete($blackListUsernames,$iFound)
    EndIf
Next
_ArrayDisplay($Usernames)
_ArrayDisplay($blackListUsernames)

_ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1.
_ArrayDisplay($Usernames)

For arrays generated from web etc. both arrays have to be unique before using the For..Next loop:

#include <Array.au3>

Local $Usernames[4] = ["Dan", "Bob", "Billy","Bob"]
$Usernames = _ArrayUnique($Usernames) ;Uniques the array.
_ArrayDelete($Usernames,0)
Local $blackListUsernames[3] = ["Dan", "Scott", "Dan"]
$blackListUsernames = _ArrayUnique($blackListUsernames) ;Uniques the array.
_ArrayDelete($blackListUsernames,0)
For $i=UBound($Usernames)-1 To 0 Step -1
    $iFound=_ArraySearch($blackListUsernames,$Usernames[$i])
    If Not @error Then
        _ArrayDelete($Usernames,$i)
        _ArrayDelete($blackListUsernames,$iFound)
    EndIf
Next
_ArrayDisplay($Usernames)
_ArrayDisplay($blackListUsernames)

_ArrayAdd($Usernames, $blackListUsernames) ;combines the 2 arrays into 1.
_ArrayDisplay($Usernames)

 

Edited by AutoBert
2 people like this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

A funny and fast way using Scripting.Dictionary  :)

#include <Array.au3>

Local $a[4] = ["Dan", "Bob", "Billy", "Bob"]
Local $b[3] = ["Dan", "Scott", "Dan"]
; create the dictionaries
Local $sda = ObjCreate("Scripting.Dictionary")
Local $sdb = ObjCreate("Scripting.Dictionary")
; populate them
For $i In $a   
    $sda.Item($i) 
Next
For $i In $b
    $sdb.Item($i)
Next
; list the wanted items
Local $sdc = ObjCreate("Scripting.Dictionary")
For $i In $a
    If not $sdb.Exists($i) Then $sdc.Item($i)
Next
For $i In $b
    If not $sda.Exists($i) Then $sdc.Item($i)
Next
; get the result array
$aResult = $sdc.Keys()
_ArrayDisplay($aResult, "$aResult")

 

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

It doesn't work when, for instance, $b is ["Dan", "Scott", "Dan"] because "Dan" remains in the final array.

Edited by jchd
Was fixed in the meantime

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Another one, based on Scripting.Dictionary :

#Include <Array.au3>

Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott"]
Local $aUniqs = _ArrayRemoveDuplicates($a)
_ArrayDisplay($aUniqs)


Func _ArrayRemoveDuplicates($aArray)
    Local $oDictionary = ObjCreate("Scripting.Dictionary")

    For $i = 0 To UBound($aArray) - 1
        $oDictionary.Item($aArray[$i]) += 1
    Next
    
    For $vVal In $oDictionary.Keys()
        If $oDictionary.Item($vVal) > 1 Then 
            $oDictionary.Remove($vVal)
        EndIf
    Next

    Return $oDictionary.Keys()
EndFunc

 

Edited by jguinch
modification to use $oDictionary.Remove

Share this post


Link to post
Share on other sites

@jchd
I saw this and edited immediately  :>

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@jguinch

<snip>
Your comment below is right, it's definitely not my day  :sweating:
 

Edited by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

#Include <Array.au3>

Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott" , "Dan" , "Bob" , "Bob" , "Dan"]

_ArraySort($a)

_ArrayDisplay(stringsplit(StringRegExpReplace(StringRegExpReplace(StringRegExpReplace(_ArrayToString($a , ",") , "(\w+?),\g1" , "$1") , "(\w+?),\g1" , "") , ",+" , ",") , "," , 2))

 

*And Btw, if you dont fix my arraydisplay with code, and instead just reply: "but it only works for that array AND looks ridiculous."  Then you are pointing out many obvious things, none of which get us any closer to a working one line display from a sorted array.

Edited by iamtheky

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

Share this post


Link to post
Share on other sites

#10 ·  Posted

This one is.......less wrong, but still operates by sheer luck.

#Include <Array.au3>

Local $a = ["Dan", "Bob", "Billy", "Dan", "Scott" , "Dan" , "Bob" , "Dan"]

_ArraySort($a)

msgbox(0, '' , StringRegExpReplace(StringRegExpReplace (_ArrayToString($a , ",") , "(\b\w+\b)\W\1" , "") , ",+" , ","))

 


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

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