Skip to content

Instantly share code, notes, and snippets.

@tsak
Created June 18, 2019 16:03
Show Gist options
  • Save tsak/6a48fdc07783384204d9165755a112ef to your computer and use it in GitHub Desktop.
Save tsak/6a48fdc07783384204d9165755a112ef to your computer and use it in GitHub Desktop.
Minimum unique array sum
package main
import (
"fmt"
)
func contains(m map[int32]struct{}, e int32) bool {
_, ok := m[e]
return ok
}
func getMinimumUniqueSum(arr []int32) int32 {
var sum int32 = 0
uniques := make(map[int32]struct{})
uniques[arr[0]] = struct{}{}
for i := 1; i < len(arr); i++ {
v := arr[i]
for contains(uniques, v) {
v++
}
uniques[v] = struct{}{}
}
for v := range uniques {
sum += v
}
return sum
}
func main() {
fmt.Println(getMinimumUniqueSum([]int32{1, 2, 2}), 6)
fmt.Println(getMinimumUniqueSum([]int32{1, 2, 3}), 6)
fmt.Println(getMinimumUniqueSum([]int32{2, 2, 4, 5}), 14)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment