Jump to content

array unique with a 2D array return?


Recommended Posts

Let's say the following is my array, I need something that will look at the third column and return a 2d array without duplicates in the third column.

Dec|four|Mon

Jan|nine|Tue

Mar|six|Wed

Bus|five|Tue

blue|four|Mon

Jan|nine|Tue

Mar|six|Sat

Bus|five|Fri

 

So this should be the result:

Dec|four|Mon

Jan|nine|Tue

Mar|six|Wed

Mar|six|Sat

Bus|five|Fri

I looked at _arrayunique, but it only returns a 1D array of the column I want to analyze for the duplicate entries. I searched but didn't find something like what I am after. Is this possible?

Edited by Champak
Link to comment
Share on other sites

  • Champak changed the title to array unique with a 2D array return?

Maybe something like this ?

#include <Array.au3>
Opt("MustDeclareVars", 1)

Local $aArray[][] = [ _
    ["Dec","four","Mon"], ["Jan","nine","Tue"], ["Mar","six","Wed"], ["Bus","five","Tue"], _
    ["blue","four","Mon"],["Jan","nine","Tue"], ["Mar","six","Sat"], ["Bus","five","Fri"] ]

Local $iColSearch = 2, $aArray2 = _ArrayUnique2D($aArray, $iColSearch)
_ArrayDisplay($aArray2, "Array Unique col " & $iColSearch)

;=============================================
Func _ArrayUnique2D(ByRef $aArray, $iColSearch)

    Local $iRows = Ubound($aArray, $UBOUND_ROWS), $iCols = Ubound($aArray, $UBOUND_COLUMNS)
    Local $aArray2[$iRows][$iCols], $iRow = -1, $oDictionary = ObjCreate("Scripting.Dictionary")

    For $i = 0 To $iRows - 1
        If Not $oDictionary.Exists($aArray[$i][$iColSearch]) Then ; key exists ?
            $oDictionary.Add($aArray[$i][$iColSearch], $i) ; key important, item not.
            $iRow += 1 ; 0+
            For $j = 0 To $iCols - 1
                $aArray2[$iRow][$j] = $aArray[$i][$j]
            Next
        EndIf
    Next

    ReDim $aArray2[$iRow + 1][$iCols]
    Return $aArray2
EndFunc

ArrayUnique2D.png.e12bfa56e4bd133affb63db204134c98.png

Link to comment
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
 Share

  • Recently Browsing   0 members

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