loading...

re: Daily Challenge #220 - What Dominates Your Array? VIEW POST

FULL DISCUSSION
 

Go Soultion

package main

import "fmt"

func dominator(arr []int) int {
    numCount := make(map[int]int)

    for _, num := range arr {
        numCount[num]++

        if numCount[num] > len(arr)/2 {
            return num
        }
    }

    return -1
}

func main() {
    for _, list := range [][]int{
        {3, 4, 3, 2, 3, 1, 3, 3},
        {1, 2, 3, 4, 5},
        {3, 4, 3, 2, 3, 1, 3, 3},
        {1, 1, 1, 2, 2, 2},
        {1, 1, 1, 2, 2, 2, 2},
    } {
        fmt.Printf("%v => %d\n", list, dominator(list))
    }
}

output:

[3 4 3 2 3 1 3 3] => 3
[1 2 3 4 5] => -1
[3 4 3 2 3 1 3 3] => 3
[1 1 1 2 2 2] => -1
[1 1 1 2 2 2 2] => 2
code of conduct - report abuse