Jump to content
Sign in to follow this  
hydroxide

_ArrayDiff()

Recommended Posts

hydroxide

Thought I'd share this with you guys. I've included an example as well.

Brute force comparison is not used here. Instead, the code steps through sorted arrays comparing them on the way.

If you wish to get only one instance of a matching element, then you could use _ArrayUnique with either the input arrays or the resultant arrays.

The result array's size will have to be greater than or equal to the size of the smaller arrays among the two which are being compared. Before returning, the result array is ReDim'ed to its size.

Credit:

Algorithm referred to from http://www.coderanch.com/t/35439/Programmi...tion-two-arrays

Cheers

#include <array.au3>

; ==[ Example ]==
Dim $a[10], $b[10], $c[10], $numbers[10]
$numbers = StringSplit("one two three four five six seven eight nine ten", " ",2)

For $x = 0 to 9
    $rand_a = Random(0,9,1)
    $rand_b = Random(0,9,1)
    $a[$x] = $numbers[$rand_a]
    $b[$x] = $numbers[$rand_b]
Next    

_ArrayDisplay($a)
_ArrayDisplay($b)
_ArrayDiff($a,$b,$c)
If @error = 12 Then Msgbox(0,"Oops..", "No elements found in common")
_ArrayDisplay($c, "Size of intersecting array: " & UBound($c))
; =====


; #FUNCTION# ====================================================================================================



================
; Name...........: _ArrayDiff
; Description ...: Returns the common elements between two arrays (Difference/Intersection)
; Syntax.........: _ArrayDiff(Byref $firstArray, Byref $secondArray, Byref $resultArray)
; Parameters ....: $firstArray - The first array
;                 $secondArray - The second array
;                 $resultArray - The resultant array
; Return values .: Success - $avArrayTarget's new size
;                 Failure - 0, sets @error to:
;                 |1 - $firstArray is not an array
;                 |2 - $secondArray is not an array
;                 |3 - $resultArray is not an array
;                 |4 - $firstArray is not a 1 dimensional array
;                 |5 - $secondArray is not a 1 dimensional array
;                 |6 - $resultArray is not a 1 dimensional array
;                 |7 - $firstArray and $secondArray are not a 1 dimensional arrays
;                 |8 - $firstArray and $resultArray are not a 1 dimensional arrays
;                 |9 - $secondArray and $resultArray are not a 1 dimensional arrays
;                 |10 - $firstArray, $secondArray and $resultArray are not a 1 dimensional arrays
;                 |11 - $resultArray's size is lesser than the lower of $firstArray and $secondArray 
;                 |12 - No elements found in common between $firstArray and $secondArray
; Author ........: HydroXidE (Praveen Shirali)
; ====================================================================================================



===========================
Func _ArrayDiff(Byref $firstArray, Byref $secondArray, Byref $resultArray)
    If Not IsArray($firstArray) Then Return SetError(1, 0, 0)
    If Not IsArray($secondArray) Then Return SetError(2, 0, 0)
    If Not IsArray($resultArray) Then Return SetError(3, 0, 0)
    Local $firstArrayrstate = 0
    If UBound($firstArray, 0) <> 1 Then $firstArrayrstate+=1
    If UBound($secondArray, 0) <> 1 Then $firstArrayrstate+=2
    If UBound($resultArray, 0) <> 1 Then $firstArrayrstate+=4
    Switch $firstArrayrstate
    Case 1;$firstArray is multi-dimensional
        Return SetError(4, 0, 0)
    Case 2;$secondArray is multi-dimensional
        Return SetError(5, 0, 0)
    Case 3;$firstArray and $secondArray are multi-dimensional
        Return SetError(7, 0, 0)
    Case 4;$resultArray is multi-dimensional
        Return SetError(6, 0, 0)
    Case 5;$firstArray and $resultArray are multi-dimensional
        Return SetError(8, 0, 0)
    Case 6;$secondArray and $resultArray are multi-dimensional
        Return SetError(9, 0, 0)
    Case 7;$firstArray, $secondArray and $resultArray are multi-dimensional
        Return SetError(10, 0, 0)
    EndSwitch
    $firstArrayrstate = UBound($firstArray)
    If $firstArrayrstate > UBound($secondArray) Then $firstArrayrstate = UBound($secondArray)
    If UBound($resultArray) < $firstArrayrstate Then Return SetError(11, 0, 0)
    Local $firstArraycount = 0
    Local $secondArraycount = 0
    Local $resultArraycount = 0
    _ArraySort($firstArray)
    _ArraySort($secondArray)
    While ($firstArraycount < UBound($firstArray) AND $secondArraycount < UBound($secondArray))
        if $firstArray[$firstArraycount] = $secondArray[$secondArraycount] Then
            $resultArray[$resultArraycount] = $firstArray[$firstArraycount]
            $firstArraycount+=1
            $secondArraycount+=1
            $resultArraycount+=1
        Elseif $firstArray[$firstArraycount] < $secondArray[$secondArraycount] Then
                $firstArraycount+=1
        Else
                $secondArraycount+=1
        EndIf
    WEnd
    If $resultArraycount > 1 Then 
        Redim $resultArray[$resultArraycount]
        Return $resultArray
    Else
        Return SetError(12,0,0)
    Endif
EndFunc;==>_ArrayDiff
Edited by hydroxide

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.