Golang: find first character in a String that doesn't repeat

jwesonga

I'm trying to write a function that returns the finds first character in a String that doesn't repeat, so far I have this:

package main

import (
    "fmt"
    "strings"
)

func check(s string) string {

    ss := strings.Split(s, "")
    smap := map[string]int{}

    for i := 0; i < len(ss); i++ {
        (smap[ss[i]])++

    }

    for k, v := range smap {

        if v == 1 {
            return k
        }
    }

    return ""
}

func main() {
    fmt.Println(check("nebuchadnezzer"))

}

Unfortunately in Go when you iterate a map there's no guarantee of the order so every time I run the code I get a different value, any pointers?

OneOfOne

Using a map and 2 loops : play

func check(s string) string {
    m := make(map[rune]uint, len(s)) //preallocate the map size
    for _, r := range s {
        m[r]++
    }

    for _, r := range s {
        if m[r] == 1 {
            return string(r)
        }
    }
    return ""
}

The benfit of this is using just 2 loops vs multiple loops if you're using strings.ContainsRune, strings.IndexRune (each function will have inner loops in them).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex exclude doesn't exclude string only first character

From Dev

Find First character in string that is a letter

From Dev

Find the first character in an array string

From Dev

Alarmmanager fires first time but doesn't repeat

From Dev

Alarmmanager fires first time but doesn't repeat

From Dev

How to find the first Chinese character in a java string

From Dev

Find the first character that is not whitespace in a std::string

From Dev

Find the first non repeating character in a string

From Dev

Find the first character that is not whitespace in a std::string

From Dev

RegEx to find if string has first a character and then numbers

From Dev

find first character after a given string

From Dev

UITextField shouldChangeCharactersInRange doesn't detect first character

From Dev

NASM - printf doesn't print the first character

From Dev

Repeat string/character with (format)

From Dev

String that doesn't contain character group

From Dev

String concatenation doesn't work for comma character

From Dev

ionic / Angular : ng-repeat first element doesn't appear

From Dev

Excel VBA: search a string to find the first non-text character

From Dev

How to clear character is find first of string in SQL Server column?

From Dev

How to find the first character of a String without using any API method

From Dev

How to find index of any first character in a string but not an empty space in swift?

From Dev

find sum by removing first character from string in mysql

From Dev

How to find an index of a string(first three character) from an array

From Dev

How to find first occurence of character (not from the begining of the string) in cpp ?

From Dev

find_first_of for a unicode special character in a string by use of its byte

From Dev

Why this C program to find the first unique character in a string is too slow?

From Dev

Find the first "invalid" character in a string (cleaning phone numbers)

From Dev

how to find the first repeated occurance of the character inside the same string in c

From Dev

Find first character of string, then compare it with a symbol c++

Related Related

  1. 1

    Regex exclude doesn't exclude string only first character

  2. 2

    Find First character in string that is a letter

  3. 3

    Find the first character in an array string

  4. 4

    Alarmmanager fires first time but doesn't repeat

  5. 5

    Alarmmanager fires first time but doesn't repeat

  6. 6

    How to find the first Chinese character in a java string

  7. 7

    Find the first character that is not whitespace in a std::string

  8. 8

    Find the first non repeating character in a string

  9. 9

    Find the first character that is not whitespace in a std::string

  10. 10

    RegEx to find if string has first a character and then numbers

  11. 11

    find first character after a given string

  12. 12

    UITextField shouldChangeCharactersInRange doesn't detect first character

  13. 13

    NASM - printf doesn't print the first character

  14. 14

    Repeat string/character with (format)

  15. 15

    String that doesn't contain character group

  16. 16

    String concatenation doesn't work for comma character

  17. 17

    ionic / Angular : ng-repeat first element doesn't appear

  18. 18

    Excel VBA: search a string to find the first non-text character

  19. 19

    How to clear character is find first of string in SQL Server column?

  20. 20

    How to find the first character of a String without using any API method

  21. 21

    How to find index of any first character in a string but not an empty space in swift?

  22. 22

    find sum by removing first character from string in mysql

  23. 23

    How to find an index of a string(first three character) from an array

  24. 24

    How to find first occurence of character (not from the begining of the string) in cpp ?

  25. 25

    find_first_of for a unicode special character in a string by use of its byte

  26. 26

    Why this C program to find the first unique character in a string is too slow?

  27. 27

    Find the first "invalid" character in a string (cleaning phone numbers)

  28. 28

    how to find the first repeated occurance of the character inside the same string in c

  29. 29

    Find first character of string, then compare it with a symbol c++

HotTag

Archive