Jump to content

Calculate the most frequent value from a set of data


Gabs
 Share

Recommended Posts

The situation is this: i have, lets say, 50 different variables each containing a value. I would like to find out which one of these 50 values that is the most frequent one. I cant find it in the help file so im kinda stuck.

Example values:

1,2,3,3,3,4,4,5,6,7,8,8,8,8,8,8,8,8,9

Here there is most 8:s so 8 is the most frequent value but how do i calculate this in a program?

Link to comment
Share on other sites

  • Developers

.... but how do i calculate this in a program?

this will build an Array with all values and the number of occurences, then sort and display it...

#include<array.au3>
$input = "1,2,3,3,3,4,4,5,6,7,8,8,8,8,8,8,8,8,9"
$a_Values = StringSplit($input, ",")
Dim $a_Count[1][2]
For $x = 1 To $a_Values[0]
    $found = 0
    For $y = 1 To UBound($a_Count, 1) - 1
        If $a_Values[$x] = $a_Count[$y][0] Then
            $a_Count[$y][1] = $a_Count[$y][1] + 1
            $found = 1
        EndIf
    Next
    If $found = 0 Then
        ReDim $a_Count[$y + 1][2]
        $a_Count[$y][0] = $a_Values[$x]
        $a_Count[$y][1] = 1
    EndIf
Next
;Sort result
_ArraySort($a_Count, 1, 1, 0, 2, 1)
;display result
$msg = ""
For $y = 1 To UBound($a_Count, 1) - 1
    $msg = $msg & "Value:" & $a_Count[$y][0] & "  occured:" & $a_Count[$y][1] & @LF
Next
MsgBox(0, "result", $msg)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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