Skip to content

Instantly share code, notes, and snippets.

@kgthegreat
Last active December 17, 2020 16:26
Show Gist options
  • Save kgthegreat/1c3b45eeeb2e428bf2fbab21b93c456c to your computer and use it in GitHub Desktop.
Save kgthegreat/1c3b45eeeb2e428bf2fbab21b93c456c to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
// Assuming you have access to distinct elements. If not this needs to be done
distinct := map[string]int{"a": 0, "b": 0, "c": 0, "d": 0}
// Homework: Read input from desired
given := []string{"a", "a", "a", "b", "b", "c", "c", "c", "d", "d"}
// expected := []string{"a", "b", "c", "d", "a", "b", "c", "d", "a", "c"}
for _, v := range given {
for x, y := range distinct {
if v == x {
y = y + 1
distinct[x] = y
}
}
}
fmt.Println(distinct)
end := []string{}
//Homework - get the largest repeated element. easy enough
largestRepeat := 3
for i := largestRepeat; i > 0; i-- {
for x, y := range distinct {
if y > 0 {
end = append(end, x)
}
y = y - 1
distinct[x] = y
}
}
//Homework - Use a sorted data structure to get proper sorting if you need it. Maps in golang do not guarantee sort order
fmt.Println(end)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment