Sign in to follow this  
Followers 0
Vindicator

how to find the most occuring number in an array?

6 posts in this topic

#1 ·  Posted (edited)

oook, so for those of you who have answered my earlier myriad of questions, Im building a script that would find the mean, meadian and mode for a set of numbers... Im almost done, i just need to find out how to use arrays to find the most occuring number ( mode ), here is my attempt at this:

Dim $array[10]
    $array1=_ArraySearch($avArray,$avArray[1])
    $array2=_ArraySearch($avArray,$avArray[2])
    $array3=_ArraySearch($avArray,$avArray[3])
    $array4=_ArraySearch($avArray,$avArray[4])
    $array5=_ArraySearch($avArray,$avArray[5])
    $array6=_ArraySearch($avArray,$avArray[6])
    $array7=_ArraySearch($avArray,$avArray[7])
    $array8=_ArraySearch($avArray,$avArray[8])
    $array9=_ArraySearch($avArray,$avArray[9])
    If $array1 > 1 Then
        $array[1]=Number($array1)
    Else
        $array[1]=Number(0)
    EndIf
    
    If $array2 > 1 Then
            $array[2]=Number($array2)
    Else
        $array[2]=Number(0)
    EndIf
    
    If $array3 > 1 Then
                $array[3]=Number($array3)
    Else
        $array[3]=Number(0)
    EndIf
    
        If $array4 > 1 Then
        $array[4]=Number($array4)
    Else
        $array[4]=Number(0)
    EndIf
    
    If $array5 > 1 Then
            $array[5]=Number($array5)
    Else
        $array[5]=Number(0)
    EndIf
    
    If $array6 > 1 Then
                $array[6]=Number($array6)
    Else
        $array[6]=Number(0)
    EndIf
    
        If $array7 > 1 Then
        $array[7]=Number($array7)
    Else
        $array[7]=Number(0)
    EndIf
    
    If $array8 > 1 Then
            $array[8]=Number($array8)
    Else
        $array[8]=Number(0)
    EndIf
    
    If $array9 > 1 Then
                $array[9]=Number($array9)
    Else
        $array[9]=Number(0)
    EndIf
    _ArraySort($array,1)
    $mode=$array[1]
    GUICtrlSetData($Input_15,$mode)

*EDIT: this was failure, of course

Edited by P a R a D o X

If I hadn't said thank you yet, and you deserve it, than thank you...

Share this post


Link to post
Share on other sites



any ideas?


If I hadn't said thank you yet, and you deserve it, than thank you...

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Maybe something like:

#include <Array.au3>

Dim $a_array[20], $a_total

For $x = 0 To UBound($a_array) - 1
    $a_array[$x] = Random(1, 50, 1)
Next

$a_DupeCount = _ArrayCountOccurance($a_array)

For $x = 0 To UBound($a_total) - 1
    ConsoleWrite("Number (" & $a_DupeCount[$x][0] & ") : Count (" & $a_DupeCount[$x][1] & ")" & @LF)
Next

ConsoleWrite(@LF & "Highest: " & $a_DupeCount[0][0] & " : " & $a_DupeCount[0][1] & @LF)

Func _ArrayCountOccurance(ByRef $a_array)
    _ArraySort($a_array)
    Dim $a_total[1][2]
    $a_total[0][0] = $a_array[0]
    $a_total[0][1] = 1

    $hold = $a_array[0]
    For $x = 1 To UBound($a_array) - 1
        If $a_array[$x] = $hold Then
            $a_total[UBound($a_total) - 1][1] = $a_total[UBound($a_total) - 1][1] + 1
        Else
            ReDim $a_total[UBound($a_total) + 1][2]
            $a_total[UBound($a_total) - 1][0] = $a_array[$x]
            $a_total[UBound($a_total) - 1][1] = 1
            $hold = $a_array[$x]
        EndIf
    Next
    _ArraySort($a_total,1,0,0,2,1)
    Return $a_total
EndFunc   ;==>_ArrayCountOccurance
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Example

#include <array.au3>
Dim $aNum[11] = [1,3,8,3,4,6,2,2,1,1]
$Max = _ArrayReturnMax($aNum)
_ArrayDisplay($Max, '')oÝ÷ Ù«­¢+ÙÕ¹}ÉÉåIÑÕɹ5à ÀÌØíÉÉä°ÀÌØí¥  ÍôÀ¤($ìÀÌØí¥  ÍÉÉ䥹àÍÑÉÑÌÐÀäÕ±Ð($íÉÑÕɹÌÄ¥´ÉÉäÝ¥Ñ ¡¥¡ÍС½Õ±µÕ±Ñ¥Á±Ì¥Ñ¡äÁÁÈѡ͵µ½Õ¹Ð½Ñ¥µÌ¤(%%9½Ð%ÍÉÉä ÀÌØíÉÉä¤Q¡¸IÑÕɸMÑÉÉ½È Ä°À°À¤(%1½°ÀÌØíÍѽMÑÈôÌäìÌäì(%½ÈÀÌØí¥
ôÀÌØí¥    ÍQ¼U  ½Õ¹ ÀÌØíÉÉ䤴Ä($$ÀÌØíÍѽMÑȵÀìôÀÌØíÉÉålÀÌØí¥

tµÀì
¡È Ĥ(%9áÐ(%1½°ÀÌØíÍ!½±°ÀÌØí¹!¥ ôÀ°ÀÌØí¹áÐôÀ(%½ÈÀÌØí¥
ôÀÌØí¥    ÍQ¼U  ½Õ¹ ÀÌØíÉÉ䤴Ä($%MÑÉ¥¹IÁ± ÀÌØíÍѽMÑÈ°MÑÉ¥¹ ÀÌØíÉÉålÀÌØí¥

t¤°ÌäìÌäì¤($$ÀÌØí¹áÐôáѹ($%%ÀÌØí¹áÐÐìôÀÌØí¹!¥ ¹9½Ð|($$$%MÑÉ¥¹%¹MÑÈ¡
¡È ĤµÀìÀÌØíÍ!½±°
¡È ĤµÀìÀÌØíÉÉålÀÌØí¥

tµÀì
¡È Ĥ°Ä¤Q¡¸($$%%ÀÌØí¹áÐôÀÌØí¹!¥ Q¡¸($$$$ÀÌØíÍ!½±µÀìôÀÌØíÉÉålÀÌØí¥

tµÀì
¡È Ĥ($$%±Í($$$$ÀÌØíÍ!½±ôÀÌØíÉÉålÀÌØí¥

tµÀì
¡È Ĥ($$%¹%($$$ÀÌØí¹!¥ ôÀÌØí¹áÐ($%¹%(%9áÐ(%%ÀÌØíÍ!½±Q¡¸IÑÕɸMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹QÉ¥µI¥¡Ð ÀÌØíÍ!½±°Ä¤°
¡È Ĥ¤(%IÑÕɸMÑÉÉ½È È°À°À¤)¹Õ¹

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Here's another way showing it returning a 2 dim array [0] has the item that was found the most (could be multiple) and [1] holds the number of times it was found

Dim $aNum[11] = [1,3,8,2,4,6,2,2,1,1]
$Max = _ArrayReturnMax($aNum)
_ArrayDisplay2D($Max, '')
Func _ArrayReturnMax($aArray, $iBase = 0)
    ;$iBase Array index starts at 0 by default
    ;returns a 2 dim array with [0] holds item found [1] number of times
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sAtoStr = ''
    For $iCC = $iBase To UBound($aArray) - 1
        $sAtoStr &= $aArray[$iCC] & Chr(1)
    Next
    Local $nHigh = 0, $nExt = 0, $aTemp[1][2], $iAdd, $sHold
    For $iCC = $iBase To UBound($aArray) - 1
        StringReplace($sAtoStr, String($aArray[$iCC]), '')
        $nExt = @extended
        If $nExt >= $nHigh And Not _
                StringInStr(Chr(1) & $sHold, Chr(1) & $aArray[$iCC] & Chr(1), 1) Then
            If $nExt = $nHigh Then
                $iAdd += 1
                ReDim $aTemp[$iAdd + 1][2]
                $aTemp[$iAdd][0] = $aArray[$iCC]
                $aTemp[$iAdd][1] = $nExt
                $sHold &= $aArray[$iCC] & Chr(1)
            Else
                $iAdd = 1
                ReDim $aTemp[$iAdd + 1][2]
                $aTemp[$iAdd][0] = $aArray[$iCC]
                $aTemp[$iAdd][1] = $nExt
                $sHold = $aArray[$iCC] & Chr(1)
            EndIf
            $nHigh = $nExt
        EndIf
    Next
    If $iAdd Then Return $aTemp
    Return SetError(2, 0, 0)
EndFunc

Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) -1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    For $iCC = $iBase To UBound($aArray, 1) - 1
        For $xCC = 0 To UBound($aArray, 2) - 1
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

uhh, yea thanks smok n for reminding me I never replied to this thread... I lost the thread after about a day... and yea that did help, thanks!


If I hadn't said thank you yet, and you deserve it, than thank you...

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