Interview Question

Software Design Engineer Interview Seattle, WA

Given a string find the first non-repeated character.

Answer

Interview Answer

9 Answers

1

Hint: use a hash table

Interview Candidate on Mar 19, 2009
0

public static char getFirstNonRepeatedChar(String s) {
        List<Character> charList = null;
        char nonRepeatedChar ='?';
        if (s != null) {
            s = s.trim();
            charList = new ArrayList<Character>();
            for (int i=0; i<s.length(); i++) {
                Character c = s.charAt(i);
                if (!charList.contains(c)) {
                    charList.add(c);
                } else {
                    charList.remove(c);
                }
            }
        }
        if (charList != null && !charList.isEmpty()) {
            nonRepeatedChar = charList.get(0);
        }
        return nonRepeatedChar;
    }

Rajiv on Aug 23, 2010
1

@Rajiv : Your solution is completely wrong. It will fail for input of "aaa"
Reason: on first check, you insert "a". On next check you remove it. On next check you again insert it and return that as your answer, even though it was repeated thrice.

Kumar Manish on Feb 13, 2011
0

hash of non repeating characters tied to a double linked list, remove any repeating character from the hash and the list. at the end the head of the list is the answer. you can use the same hash to keep the counters.

dantepy on Apr 30, 2011
0

public static String findFirstNonRepeatedCharacter(String S) {

        int[] T = new int[256];
        for (int i = 0; i < S.length(); i++) {
            char next = S.charAt(i);
            T[next] = T[next] + 1;
        }

        for (int i = 0; i < S.length(); i++) {
            if(T[S.charAt(i)] == 1)
                return String.valueOf(S.charAt(i));
        }

        return null;
    }

AB on Jun 14, 2012
0

Three ways to find first non repeating character in a string c#

find the code below -

public char firstNonRepetitive(string inputString)
{
    int nextOccurrence = 0;
    char firstDistinctChar = ' ';
    for (int i = 0; i < inputString.Length; i++)
    {
        nextOccurrence = 0;
        for (int j = (i + 1); j < inputString.Length; j++)
        {
            if (inputString[i] == inputString[j])
                nextOccurrence++;
        }
        if (nextOccurrence == 0)
        {
            firstDistinctChar = inputString[i];
            break;
        }
    }
    return firstDistinctChar;

}
Check out amazing two more way -

program to find first non repeating character in a string c# - three ways

http://www.dotnetbull.com/2013/08/find-first-non-repeating-character-string.html

Vivek Gupta on Aug 15, 2013
0

<a href='http://www.dotnetbull.com/2013/08/find-first-non-repeating-character-string.html' >program to find first non repeating character in a string c# - three ways<a>

Deepti Thakur on Aug 15, 2013
0

My python implementation:

def firstNonRepeatingCharacter(inputString):
    hashmap = {}
    for x in inputString:
        if (x in hashmap):
            hashmap[x] = hashmap[x] + 1
        else:
            hashmap[x] = 1
    for x in inputString:
        if (hashmap[x] > 1):
            continue
        else:
            return x
    return "No nonrepeating character found"

Sina Yeganeh on Sep 5, 2013
0

for (int index = 0; index < str.Length; index++)
            {
                if (str.LastIndexOf(str[index]) == str.IndexOf(str[index]))
                {
                    return str[index].ToString();

                }
            }

poorni on Mar 23, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.