DEV Community

Cover image for Coding Problem Interview Series: How to Find the First Non-Repeated Character in a String?
Ben Halpern Subscriber for CodeNewbie

Posted on

Coding Problem Interview Series: How to Find the First Non-Repeated Character in a String?

๐Ÿ“ฃ Calling experienced devs and recent interviewees! Join the "Coding Problem Interview Series" to help code newbies tackle interview questions assessing problem-solving skills, algorithmic knowledge, and implementation of sorting, string manipulation, and data structure algorithms.

Share your expertise and insights! Pleas share multiple perspectives and tips for standout answers!

Today's question is:

Could you describe how you would find and print the first non-repeated character in a string?

Follow the CodeNewbie Org and #codenewbie for more discussions and online camaraderie!

Top comments (3)

Collapse
 
danielrendox profile image
Daniel Rendox

Damn, it's more difficult then I thought! How about this solution in Java? I hope it's not dumb.

private static char findFirstNonRepeatedChar(String word) {
    char[] wordArray = word.toCharArray();
    char experimental;
    boolean repeated;

    for (int index = 0; index < wordArray.length; index++){
    repeated = false;
    experimental = wordArray[index];
    for (int i = 0; i < wordArray.length; i++) {
        if (i != index && wordArray[i] == experimental) {
            repeated = true;
            break;
        }
    }
    if (!repeated) return wordArray[index];
        }
    return '\u0000';
}
Enter fullscreen mode Exit fullscreen mode

It's easier to check whether the character is repeated first. For this, we need to save the first char and loop through the rest of the string, looking for a similar one. Repeat for the 2nd, 3rd, and other chars. So it's a nested loop.

Then we need to add a boolean. If we find a repeated char, set the boolean to true, but if not, the condition in the outer loop will return from the method.

It's worth noticing that we need to :

  • add i != index to check if the experimental char is not the same char in the word;
  • set the repeated value to false every iteration because otherwise, it won't find any non-repeated chars in the word that starts with double letters e.g. "eerie";
  • return a default char value if the method didn't find any non-repeated chars.
Collapse
 
jstrawther profile image
Justin Strawther

This can be done with two iterations.

  • Declare a hash table or a dictionary to map characters to number of occurrences.
  • Iterate the string, adding each encountered character to the hash table with a value of 1, or incrementing the value if the character already exists in the table.
  • Iterate the string again, checking the hash table for the count of each character. Stop when you find a character with a value of 1.
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited
// Remove any repeated characters, first non-repeated is first of what remains
const firstNonRepeated = str => str.replace(/([^])\1+/g,'')[0]
Enter fullscreen mode Exit fullscreen mode