How to count only the unique words from a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "go c c++ java c++ go php rust rust go java"

	words := strings.Split(str, " ")
	uniqueWords := make(map[string]bool)

	for _, word := range words {
		uniqueWords[word] = true
	}

	totalUniqueWords := len(uniqueWords)

	fmt.Println(totalUniqueWords)
}


/*
run:

6

*/

 



answered Sep 20, 2024 by avibootz
...