Jump to content

Count Element Recurrence In An Array


exodius
 Share

Recommended Posts

This function will count the number of times that an element occurs in a single dimension array and then returns a 2-dimensional array where the first element is the element that was counted and the second element is the number of times it occurred.

It will give the following message if you run it from SciTE, but it will go ahead and work.. You don't get this error if you compile it and run it. It's just complaining about the global declaration not being outside of the function.

For $x = 0 To UBound($EnumerateArray) - 1

For $x = 0 To UBound(^ ERROR

#include <Array.au3>

Func ArrayCountElementRecurrence($Array)
    Global $EnumerateArray[UBound($Array) - 1][2]
    
    $SameCount = 1
    $Progress = 0
    For $x = 0 To UBound($Array) - 1
        _ArraySort($Array)
        If $x + 1 <= UBound($Array) - 1 Then
            If $Array[$x] = $Array[$x + 1] Then
                $SameCount = $SameCount + 1
                ContinueLoop
            EndIf
        EndIf
        
        If $x + 1 <= UBound($Array) - 1 Then
            If $Array[$x] <> $Array[$x + 1] Then
                $EnumerateArray[$Progress][0] = $Array[$x]
                $EnumerateArray[$Progress][1] = $SameCount
                $SameCount = 1
                $Progress = $Progress + 1
            EndIf
        Else
            $EnumerateArray[$Progress][0] = $Array[$x]
            $EnumerateArray[$Progress][1] = $SameCount
            $SameCount = 1
            $Progress = $Progress + 1
        EndIf
    Next
EndFunc ;==>ArrayCountElementRecurrence

For $x = 0 To UBound($EnumerateArray) - 1
    
    If $EnumerateArray[$x][0] = 0 And $EnumerateArray[$x][1] = 0 Then ContinueLoop
    
    MsgBox(0, $EnumerateArray[$x][0], $EnumerateArray[$x][1])
Next
Edited by exodius
Link to comment
Share on other sites

I got an error when running your code... i had somethign liek this written a whiel ago and tweaked it a little to make it liek yours. I use a local 2d array and then return the 2d array from the function.

For $x = 0 To UBound($EnumerateArray) - 1

For $x = 0 To UBound(^ ERROR

#include <Array.au3>

Func ArrayCountElementRecurrence($Array)
    Local $disp_array[UBound($Array)][2]
    
    For $iter1 = 0 To UBound( $Array ) - 1
        $count = 0
        $data = $Array[$iter1]
        $cont_loop = 0
        
        For $iter3 = 0 To UBound( $Array ) - 1
            If $data = $disp_array[$iter3][0] Then
                $cont_loop = 1
            EndIf
        Next
        
        If $cont_loop = 1 Then  
            ContinueLoop
        EndIf
        
        $disp_array[$iter1][0] = $Array[$iter1]
        
        For $iter2 = 0 To UBound( $Array ) - 1
            If $data = $Array[$iter2] Then  
                $count = $count + 1
            EndIf
        Next
        $disp_array[$iter1][1] = $count
    Next
    
    Return $disp_array
EndFunc;==>ArrayCountElementRecurrence

$Array = _ArrayCreate( "1", "1", "4", "a", "zzzz", "a", "4" )

$result = ArrayCountElementRecurrence( $Array )

$output = ""

For $iter = 1 To UBound($Array)
    if $result[$iter - 1][0] <> "" Then
        $output = $output & $result[$iter - 1][0] & " occurs " & $result[$iter - 1][1] & " time(s)." & @CR
    EndIf
Next

MsgBox( 0, "", $output )

EDIT: foprgot to include error

Edited by Don N

_____________________________________________________"some people live for the rules, I live for exceptions"Wallpaper Changer - Easily Change Your Windows Wallpaper

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...