{"record":{"id":"96a22837307684fa","repo":"TheAlgorithms/Go","slug":"empty-slice-provided","errorCode":null,"errorMessage":"empty slice provided","messagePattern":"empty slice provided","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"math/mode.go","lineNumber":19,"sourceCode":"// mode.go\n// author(s): [CalvinNJK] (https://github.com/CalvinNJK)\n// time complexity: O(n)\n// space complexity: O(n)\n// description: Finding Mode Value In an Array\n// see mode.go\n\npackage math\n\nimport (\n\t\"errors\"\n\n\t\"github.com/TheAlgorithms/Go/constraints\"\n)\n\n// ErrEmptySlice is the error returned by functions in math package when\n// an empty slice is provided to it as argument when the function expects\n// a non-empty slice.\nvar ErrEmptySlice = errors.New(\"empty slice provided\")\n\nfunc Mode[T constraints.Number](numbers []T) (T, error) {\n\n\tcountMap := make(map[T]int)\n\n\tn := len(numbers)\n\n\tif n == 0 {\n\t\treturn 0, ErrEmptySlice\n\t}\n\n\tfor _, number := range numbers {\n\t\tcountMap[number]++\n\t}\n\n\tvar mode T\n\tcount := 0\n","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/mode.go#L1-L37","documentation":"ErrEmptySlice is the sentinel error returned by math functions (e.g. Mode) that require at least one element; Mode returns (0, ErrEmptySlice) when len(numbers) == 0. As a declared package-level error, callers can compare with errors.Is / equality.","triggerScenarios":"Calling math.Mode with an empty slice: Mode([]int{}) or Mode(someSlice) where the slice was never populated or was filtered down to zero elements.","commonSituations":"Reading an empty CSV/file into a slice; filtering that removes all items; a config list left empty; early-return paths where the caller forgot to check length before computing statistics.","solutions":["Check len(numbers) > 0 before calling Mode and handle the empty case explicitly.","Use errors.Is(err, math.ErrEmptySlice) to detect it and return a domain-appropriate default.","Fix upstream data loading so the slice is actually populated."],"exampleFix":"// before\nmode, err := math.Mode(values) // err = ErrEmptySlice\n\n// after\nif len(values) == 0 {\n    return 0, nil // or a domain default\n}\nmode, err := math.Mode(values)","handlingStrategy":"validation","validationCode":"if len(values) == 0 {\n    return 0, nil // define a default for the empty case\n}\nmode, err := math.Mode(values)","typeGuard":"func nonEmpty[T any](xs []T) bool {\n    return len(xs) > 0\n}","tryCatchPattern":"mode, err := math.Mode(values)\nif errors.Is(err, math.ErrEmptySlice) {\n    return 0, nil // or propagate a domain-specific empty result\n}\nif err != nil {\n    return 0, err\n}","preventionTips":["Check slice length after every filter/map step before statistics calls.","Treat empty inputs as a defined case in your API, not an afterthought.","Compare with errors.Is against the exported sentinel, not string matching."],"tags":["math","empty-slice","sentinel-error","go"],"backgroundTag":"empty-input-collection","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}