github.com/blend/go-sdk@v1.20220411.3/mathutil/mode.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package mathutil 9 10 // Mode gets the mode of a slice of numbers 11 // `Mode` generally is the most frequently occurring values within the input set. 12 func Mode(input []float64) []float64 { 13 l := len(input) 14 if l == 1 { 15 return input 16 } else if l == 0 { 17 return []float64{} 18 } 19 20 m := make(map[float64]int) 21 for _, v := range input { 22 m[v]++ 23 } 24 25 mode := []float64{} 26 27 var current int 28 for k, v := range m { 29 switch { 30 case v < current: 31 case v > current: 32 current = v 33 mode = append(mode[:0], k) 34 default: 35 mode = append(mode, k) 36 } 37 } 38 39 lm := len(mode) 40 if l == lm { 41 return []float64{} 42 } 43 44 return mode 45 }