Jump to content
Sign in to follow this  
darzanmihai

Need help...again---the item with max numer of occurences in array

Recommended Posts

darzanmihai

I would like to get from an 1D array the value that occurs often then the other values of the array....how could I do this, without complicating the script?

Thanks in advance!


I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Share this post


Link to post
Share on other sites
weaponx

Quick & dirty. This could be optimized depending on how many elements you are dealing with. Unique values only need to be search once but this will search the entire array for every element, even if it has already been searched. A hash table would need to be created to store values that had already been searched.

Dim $array[10]

$array[0] = "Blue"
$array[1] = "Blue"
$array[2] = "Blue"
$array[3] = "Green"
$array[4] = "Green"
$array[5] = "Green"
$array[6] = "Red"
$array[7] = "Red"
$array[8] = "Red"
$array[9] = "Blue"

MsgBox(0,"Most frequent value", ArrayMode($array))

Func ArrayMode($aArray)
    Local $Numelements = Ubound($aArray)
    
    Local $Highest_Count = 0
    Local $mMode
    
    ;Loop through all elements, totalling up occurences
    For $X = 0 to $Numelements-1
        $Count = 0
        
        For $Y = 0 to $Numelements-1
            If $Array[$Y] = $aArray[$X] Then $Count += 1
        Next
        
        If $Count > $Highest_Count Then
            $Highest_Count = $Count
            $mMode = $aArray[$X]
        EndIf
    Next
    
    Return $mMode
EndFunc
Edited by weaponx

Share this post


Link to post
Share on other sites
darzanmihai

Quick & dirty. This could be optimized depending on how many elements you are dealing with. Unique values only need to be search once but this will search the entire array for every element, even if it has already been searched. A hash table would need to be created to store values that had already been searched.

Dim $array[10]

$array[0] = "Blue"
$array[1] = "Blue"
$array[2] = "Blue"
$array[3] = "Green"
$array[4] = "Green"
$array[5] = "Green"
$array[6] = "Red"
$array[7] = "Red"
$array[8] = "Red"
$array[9] = "Blue"

MsgBox(0,"Most frequent value", ArrayMode($array))

Func ArrayMode($aArray)
    Local $Numelements = Ubound($aArray)
    
    Local $Highest_Count = 0
    Local $mMode
    
    ;Loop through all elements, totalling up occurences
    For $X = 0 to $Numelements-1
        $Count = 0
        
        For $Y = 0 to $Numelements-1
            If $Array[$Y] = $aArray[$X] Then $Count += 1
        Next
        
        If $Count > $Highest_Count Then
            $Highest_Count = $Count
            $mMode = $aArray[$X]
        EndIf
    Next
    
    Return $mMode
EndFunc
Thanks Weaponx for both replies

I do not like stupid and idiot people that write idiot things...If you are one, do not write.

Share this post


Link to post
Share on other sites
TerarinKerowyn

This code doesn't tell you if you have two equal to each other though, only the first occurance is what get it going. If you have 4 Red and 4 Blue with 1 Green should the state say Red / Blue? This doesn't work that way it first looks what the [0] state is. Might it be Blue or Red it will say whatever the [0] state is else if it is green then [1] state is.


Contact via MSN: [email=terarink_msn@hotmail.com]terarink_msn@hotmail.com[/email], yahoo: terarink_yah

Share this post


Link to post
Share on other sites
weaponx

This code doesn't tell you if you have two equal to each other though, only the first occurance is what get it going. If you have 4 Red and 4 Blue with 1 Green should the state say Red / Blue? This doesn't work that way it first looks what the [0] state is. Might it be Blue or Red it will say whatever the [0] state is else if it is green then [1] state is.

That is outside the scope of what was requested. If you needed to know the number of occurrences of every unique value it would have to return a 2-dimensional array with the value in [x][0] and the count in [x][1] (or use a hash table).

Here is how that would look:

#include <GUIConstantsEx.au3>
#include <Listviewconstants.au3>

Dim $array[10]

$array[0] = "Blue"
$array[1] = "Blue"
$array[2] = "Blue"
$array[3] = "Green"
$array[4] = "Green"
$array[5] = "Green"
$array[6] = "Red"
$array[7] = "Red"
$array[8] = "Red"
$array[9] = "Blue"

GUICreate("Array Value Occurrences", 200)  ; will create a dialog box that when displayed is centered
GUISetState (@SW_SHOW)       ; will display an empty dialog box

$Obj = ArrayGroup($array)

$listview = GUICtrlCreateListView ("Value | Occurrences",10,10,180,380,$LVS_SORTASCENDING)
For $key In $Obj.Keys()
    GUICtrlCreateListViewItem($key & "|" & $Obj.Item($key),$listview)
Next

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend

Func ArrayGroup($aArray)
    Local $oObj = ObjCreate("Scripting.Dictionary")
    Local $Numelements = Ubound($aArray)
   
    For $X = 0 to $Numelements - 1
        $oObj($aArray[$X]) = $oObj($aArray[$X]) + 1
    Next
    Return $oObj
EndFunc
Edited by weaponx

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.