Skip to content

How to Find the First Non-Repeating Character in Golang

The challenge

Write a function named first_non_repeating_letter that takes a string input, and returns the first character that is not repeated anywhere in the string.

For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the string.

As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS' should return 'T'.

If a string contains all repeating characters, it should return an empty string ("") or None — see sample tests.

The solution in Golang

Option 1:

package solution import ( "strings" ) func FirstNonRepeating(str string) string { for _, c := range str { if strings.Count(strings.ToLower(str), strings.ToLower(string(c))) < 2 { return string(c) } } return "" }
Code language: Go (go)

Option 2:

package solution func FirstNonRepeating(str string) string { seen := make(map[rune]int) for _, r := range str { seen[r|32]++ } for _, r := range str { if seen[r|32] == 1 { return string(r) } } return "" }
Code language: Go (go)

Option 3:

package solution import "strings" func FirstNonRepeating(str string) string { s := strings.ToLower(str) for i := ; i < len(s); i++ { st := string(s[i]) if strings.Index(s, st) == strings.LastIndex(s, st) { return string(str[i]) } } return "" }
Code language: Go (go)

Test cases to validate our solution

package solution_test import ( . "" . "" ) var _ = Describe("Basic Tests", func() { It("should handle simple tests", func() { Expect(FirstNonRepeating("a")).To(Equal("a")) Expect(FirstNonRepeating("stress")).To(Equal("t")) Expect(FirstNonRepeating("moonmen")).To(Equal("e")) }) It("should handle empty strings", func() { Expect(FirstNonRepeating("")).To(Equal("")) }) It("should handle all repeating strings", func() { Expect(FirstNonRepeating("abba")).To(Equal("")) Expect(FirstNonRepeating("aa")).To(Equal("")) }) It("should handle odd characters", func() { Expect(FirstNonRepeating("~><#~><")).To(Equal("#")) Expect(FirstNonRepeating("hello world, eh?")).To(Equal("w")) }) It("should handle letter cases", func() { Expect(FirstNonRepeating("sTreSS")).To(Equal("T")) Expect(FirstNonRepeating("Go hang a salami, I'm a lasagna hog!")).To(Equal(",")) }) })
Code language: Go (go)

See also  Alternate Capitalization in Golang
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x