如何检查具有相同字符的单词,其中一个变量中的单词

问题描述:

I'm thinking up about how I find the same characters in one variable looks like this:

var words string = "abab"

and then I want's to eliminate the same characters in that one variable and here's the output to be

Output:

ab

have any solution about this?

我正在考虑如何在一个变量中找到相同的字符,如下所示: p> \ n

 可变词string =“ abab” 
  code>  pre> 
 
 

,然后我想消除该变量中的相同字符,这是输出 成为 p>

输出: p>

  ab 
  code>  pre> 
 
 

有任何解决方案 关于这个? p> div>

One solution can be the use of go map[] to track the taken characters.

sample code:

func main() {
    s := "abcdaabcefgahccij"
    newS := ""
    taken := make(map[rune]int)
    for _, value := range s {
        if _, ok := taken[value]; !ok {
            taken[value] = 1
            newS += string(value)
        }
    }
    fmt.Println(newS)
}

Output:

abcdefghij