0

below code is printing out all characters [d, e] with counter one but I need just first character to print out, how can I do that?

def firstNonRepeat(s):
  counter = {}

  for char in s:
    if char in counter:
      counter[char] += 1
    else:
      counter[char] =  1

  for char in s:
    if counter[char] == 1:
      print char
  return None

firstNonRepeat('aabccbdcbe')
2
  • 3
    Replace print with return...your code still remains buggy tho Commented Apr 3, 2017 at 23:14
  • string.count in a loop, break when count == 1. Will give more complete answer tommorow as i am using my phone. But have a look at that suggestion. Commented Apr 3, 2017 at 23:22

7 Answers 7

1

Try this: remove that one instance (character) from the string, and see whether the character exists in the remainder:

def firstNonRepeat(s):

  for i, char in enumerate(s):
    # print i, char, s[i+1:]
    if char not in s[:i]+s[i+1:]:
      print char
      return

firstNonRepeat('aabccbdcbe')
Sign up to request clarification or add additional context in comments.

Comments

1

Use such option:

def firstNonRepeat(s):
    counter = {}
    for char in s:
        if char in counter:
            counter[char] += 1
        else:
            counter[char] =  1
    for item in counter:
        if counter[item] == 1:
            return item
print(firstNonRepeat('aabccbdcbe'))

Idea is to return the first match in for loop and stop loop by returning the match. Also if the for loop will not find anything, it will return None, there is no need to write that last return.

2 Comments

collections.Counter does not preserve order. That may be a disadvantage.
@zondo, yes, just checked that. Thanks for noting, updating question.
1

@Zorro: Your solution is almost correct. Just do a minor change in your code and you are good to go.You have to write the return statement as it finds a character with count 1. I am writing the solution for you below:

    def firstNonRepeat(s):
        counter = {}

        for char in s:
            if char in counter:
                counter[char] += 1
            else:
                counter[char] =  1

        for char in s:
            if counter[char] == 1:
                print (char)
                return

    firstNonRepeat('aabccbdcbe')

Comments

0

Use break when you find the character you're looking for to stop the for loop.

for char in s:
    if counter[char] == 1:
      print char
      break

Comments

0

Okay so first, I may have misinterpreted what you were looking for but if you are looking for a character that does not immediately repeat ('aa') then this should work for you.

You are iterating over the entire array one and a portion. I did it like this:

def first_non_repeat(s):
        if len(s) == 1:
                return s
        for i in range(len(s)):
                if (i == 0 and s[i+1] != s[i]) or (i<len(s)-1 and s[i-1] != s[i] and s[i] != s[i+1]) or (i == len(s)-1 and s[i-1] != s[i]):
                        return s[i] # or return i
        return None

It loops over the array but only as much as it needs to in order to get the first non-repeated letter. The if statement checks if the character behind and in front of the current index don't match the one at the current index, if so it returns that letter. Note, you could also have it return the index which would potentially be more useful.

If you put in

"HHHHELLLO WORLD!"

the output from the function is 'E' or if you changed it to return index value it will return 4.

Comments

0
def firstNonRepeat(s):
    counter = {}
    result = []


    for char in s:
        if char in counter:
            counter[char] += 1
        else:
            counter[char] =  1
            result.append(char)

    for i in result:
        if counter[i] == 1:
            return(i)

Comments

0

def findFirstUniqueChar(s): d = {} for c in s: if c in d: d[c] += 1 else: d[c] = 1

for c in s:
    if d[c] == 1:
        return s.index(c)
return -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.