题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
package main
import (
"fmt"
"sort"
)
func main() {
var input string
myMap := make(map[byte]int)
fmt.Scan(&input)
//使用byte比较方便
inputByt := []byte(input)
//利用map存储每个字符出现的次数
for _, val := range inputByt {
myMap[val]++
}
var times []int
//将map里面所有字符的次数存在times切片里,然后排序
for _, val := range myMap {
times = append(times, val)
}
sort.Ints(times)
//最小的次数肯定是在第一位
minTimes := times[0]
shouldBeDel := make(map[byte]int)
//再次遍历myMap把字符次数等于minTimes的字符都存储在shouldBeDel这个map里
for key, times := range myMap {
if times == minTimes {
shouldBeDel[key]=0
}
}
//遍历inputByt,判断字符是否存在于shouldBeDel这个map里,如果在里面就不打印
for _, val := range inputByt {
_, ok := shouldBeDel[val]
if ok {
continue
}
fmt.Print(string(val))
}
}
