MCPcopy
hub / github.com/montanaflynn/stats / Mode

Function Mode

mode.go:4–47  ·  view source on GitHub ↗

Mode gets the mode [most frequent value(s)] of a slice of float64s

(input Float64Data)

Source from the content-addressed store, hash-verified

2
3// Mode gets the mode [most frequent value(s)] of a slice of float64s
4func Mode(input Float64Data) (mode []float64, err error) {
5 // Return the input if there's only one number
6 l := input.Len()
7 if l == 1 {
8 return input, nil
9 } else if l == 0 {
10 return nil, EmptyInputErr
11 }
12
13 c := sortedCopyDif(input)
14 // Traverse sorted array,
15 // tracking the longest repeating sequence
16 mode = make([]float64, 5)
17 cnt, maxCnt := 1, 1
18 for i := 1; i < l; i++ {
19 switch {
20 case c[i] == c[i-1]:
21 cnt++
22 case cnt == maxCnt && maxCnt != 1:
23 mode = append(mode, c[i-1])
24 cnt = 1
25 case cnt > maxCnt:
26 mode = append(mode[:0], c[i-1])
27 maxCnt, cnt = cnt, 1
28 default:
29 cnt = 1
30 }
31 }
32 switch {
33 case cnt == maxCnt:
34 mode = append(mode, c[l-1])
35 case cnt > maxCnt:
36 mode = append(mode[:0], c[l-1])
37 maxCnt = cnt
38 }
39
40 // Since length must be greater than 1,
41 // check for slices of distinct values
42 if maxCnt == 1 || len(mode)*maxCnt == l && maxCnt != l {
43 return Float64Data{}, nil
44 }
45
46 return mode, nil
47}

Callers 8

TestModeFunction · 0.92
BenchmarkRegularAPIFunction · 0.92
mainFunction · 0.92
ModeMethod · 0.85

Calls 2

sortedCopyDifFunction · 0.85
LenMethod · 0.80

Tested by 6

TestModeFunction · 0.74
BenchmarkRegularAPIFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…