Sign in to follow this  
Followers 0
gcue

remove array duplicates in a 2d array

5 posts in this topic

#1 ·  Posted (edited)

im trying to remove a duplicate entry on a two dimensional array

entries "bob|canada" is the dupe (first and last elements in the array)

thanks in advance

#include <array.au3>

local $user_list[5][2]

$user_list[0][0] = "bob"
$user_list[0][1] = "canada"
$user_list[1][0] = "larry"
$user_list[1][1] = "usa"
$user_list[2][0] = "mary"
$user_list[2][1] = "usa"
$user_list[3][0] = "bob"
$user_list[3][1] = "mexico"
$user_list[4][0] = "bob"
$user_list[4][1] = "canada"

_ArrayDisplay($user_list, "before")
$people = _ArrayUnique($user_list)
_ArrayDisplay($people, "after")
Edited by gcue

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

i found a udf by randallac _arraydupes

http://www.autoitscript.com/forum/index.php?showtopic=65755&view=findpost&p=488237

very neat!

but it doesnt handle 2d arrays..

help!

Edited by gcue

Share this post


Link to post
Share on other sites

im trying to remove a duplicate entry on a two dimensional array

entries "bob|canada" is the dupe (first and last elements in the array)

thanks in advance

#include <array.au3>

local $user_list[5][2]

$user_list[0][0] = "bob"
$user_list[0][1] = "canada"
$user_list[1][0] = "larry"
$user_list[1][1] = "usa"
$user_list[2][0] = "mary"
$user_list[2][1] = "usa"
$user_list[3][0] = "bob"
$user_list[3][1] = "mexico"
$user_list[4][0] = "bob"
$user_list[4][1] = "canada"

_ArrayDisplay($user_list, "before")
$people = _ArrayUnique($user_list)
_ArrayDisplay($people, "after")

Here is one method that appears to work.

#include <array.au3>

Local $user_list[5][2]

$user_list[0][0] = "bob"
$user_list[0][1] = "canada"
$user_list[1][0] = "larry"
$user_list[1][1] = "usa"
$user_list[2][0] = "mary"
$user_list[2][1] = "usa"
$user_list[3][0] = "bob"
$user_list[3][1] = "mexico"
$user_list[4][0] = "bob"
$user_list[4][1] = "canada"

Local $aAnotherArray[5][4] = [[1, 2, 3, 4],[3, 4, 5, 6],[5, 6, 7, 8],[1, 2, 3, 4],[6, 4, 3, 2]] ; Another test array

_ArrayDisplay($user_list, "before")
Local $aUnique = _ArrayUnique2d($user_list)
_ArrayDisplay($aUnique, "after")

_ArrayDisplay($aAnotherArray, "before")
Local $aUnique = _ArrayUnique2d($aAnotherArray)
_ArrayDisplay($aUnique, "after")


Func _ArrayUnique2d($aArrayIn)
    ;convert $aArrayIn to one dinensiomal array, $aTemp
    Local $aTemp[UBound($aArrayIn)]
    For $r = 0 To UBound($aArrayIn) - 1
        For $c = 0 To UBound($aArrayIn, 2) - 1
            $aTemp[$r] &= $aArrayIn[$r][$c] & "#"
        Next
        $aTemp[$r] = StringTrimRight($aTemp[$r], 1)
    Next

    $aUniq = _ArrayUnique($aTemp)

    ; Re-create multi-dimensional array
    StringReplace($aUniq[1], "#", "#")
    Local $Dim2 = @extended + 1
    Local $people[UBound($aUniq) - 1][$Dim2]

    For $r = 1 To UBound($aUniq) - 1
        $aSplit = StringSplit($aUniq[$r], "#")
        For $c = 0 To $aSplit[0] - 1
            $people[$r - 1][$c] = $aSplit[$c + 1]
        Next
    Next
    Return $people
EndFunc   ;==>_ArrayUnique2d

Share this post


Link to post
Share on other sites

An interesting problem that inspired me to write this function

It returns an array 1D or 2D containing only the unique rows in the same order as the original.

It probably need a bit of error checking adding to make it user proof

#include <array.au3>

local $user_list[7][2]

$user_list[0][0] = "Name"
$user_list[0][1] = "Country"
$user_list[1][0] = "bob"
$user_list[1][1] = "canada"
$user_list[2][0] = "larry"
$user_list[2][1] = "usa"
$user_list[3][0] = "mary"
$user_list[3][1] = "usa"
$user_list[4][0] = "Mary"
$user_list[4][1] = "usa"
$user_list[5][0] = "bob"
$user_list[5][1] = "mexico"
$user_list[6][0] = "Bob"
$user_list[6][1] = "canada"

_ArrayDisplay($user_list, "before")

$people = _ArrayRowsUnique($user_list,1,False)
_ArrayDisplay($people, "after case insensitive")

$people = _ArrayRowsUnique($user_list,1,True)
_ArrayDisplay($people, "after case sensitive")

Func _ArrayRowsUnique($aArray,  $iBaseRow = 0, $bCaseSensitive = False)
    Local $iRows = UBound($aArray,1)
    Local $iCols = UBound($aArray,2)
    Local $sRec = ''
    Local $idx = 0
    Local $aTemp[$iRows]

    For $i = $iBaseRow To $iRows - 1
        If $iCols > 1 Then
            $aTemp[$i] = $aArray[$i][0]
            For $j = 1 To $iCols - 1
                $aTemp[$i] &=  "|" & $aArray[$i][$j]
            Next
        Else
            $aTemp[$i] = $aArray[$i]
        EndIf
    Next

    For $i = $iBaseRow To  $iRows - 1
        For $j = $i + 1 To  $iRows - 1
            If $bCaseSensitive Then
                If $aTemp[$i] == $aTemp[$j] Then
                    For $k = $j To  $iRows - 2
                        $aTemp[$k] = $aTemp[$j+1]
                    Next
                    $iRows -= 1
                EndIf
            Else
                If $aTemp[$i] = $aTemp[$j] Then
                    For $k = $j To  $iRows - 2
                        $aTemp[$k] = $aTemp[$j+1]
                    Next
                    $iRows -= 1
                EndIf
            EndIf
        Next
    Next
ReDim $aTemp[$iRows]

If $iCols > 1 Then
    ReDim $aArray[$iRows][$iCols]
Else
    ReDim $aArray[$iRows]
EndIf
For $i = $iBaseRow To $iRows - 1
        If $iCols > 1 Then
            $aParts = StringSplit($aTemp[$i], "|",2)
            For $j = 0 To $iCols - 1
                $aArray[$i][$j] = $aParts[$j]
            Next
        Else
            $aArray[$i] = $aTemp[$i]
        EndIf
Next

Return $aArray

EndFunc   ;==>_ArrayUnique

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

both examples work great!

thanks guys.

take care.

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
Sign in to follow this  
Followers 0