..

Lonely Integer (2n+1) Golang Solution

Competitive Programming is new to me.

Sometimes I spend time improving my Golang skills on Hackerrank and came across this question.

Many solutions are on the web, but none are written with Golang. The solutions I found resolved this issue with the XOR operator in other languages, But I want to solve this differently.

func lonelyinteger(a []int32) int32 {
    intMap := make(map[int32]int32)
    for _, x := range a {
        intMap[x]++
    }
    for _, x := range a {
        if intMap[x] == 1 {
            return x
        }
    }
    return 0
}

I filled the map with the slice, and every time the loop ran, I incremented the key’s value. And all I had to do was check which key was minor.

Thanks for reading.