Last active
December 17, 2020 16:26
-
-
Save kgthegreat/1c3b45eeeb2e428bf2fbab21b93c456c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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