{"id":1037,"date":"2026-10-30T12:16:05","date_gmt":"2026-10-30T12:16:05","guid":{"rendered":"https:\/\/blog.debuggersspace.com\/?p=1037"},"modified":"2026-10-30T14:12:02","modified_gmt":"2026-10-30T14:12:02","slug":"find-the-first-non-repeating-character-in-a-string","status":"publish","type":"post","link":"https:\/\/blog.debuggersspace.com\/index.php\/2026\/10\/30\/find-the-first-non-repeating-character-in-a-string\/","title":{"rendered":"Find the first non-repeating character in a string."},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 31 Second                <\/div>\n\n            <\/div>\n<p class=\"wp-block-paragraph\">For &#8220;swiss&#8221; the answer is &#8220;w&#8221;: &#8220;s&#8221; repeats, &#8220;w&#8221; appears once and comes before &#8220;i&#8221;. The trick is that you need counts for the whole string before you can decide, so it takes two passes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Two passes with a count array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static char firstUnique(String s) {\n    int[] count = new int[256];          \/\/ extended ASCII\n    for (char c : s.toCharArray()) count[c]++;\n    for (char c : s.toCharArray()) {\n        if (count[c] == 1) return c;\n    }\n    return '\\0';                         \/\/ none found\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First pass counts, second pass walks the string in its original order and returns the first character with count 1. Time O(n), space O(1) for a fixed alphabet. Walk the <em>string<\/em> in the second pass, not the count array, or you get the alphabetically first unique character instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">LinkedHashMap version (any character set)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>static Character firstUniqueMap(String s) {\n    Map&lt;Character, Integer&gt; m = new LinkedHashMap&lt;&gt;();\n    for (char c : s.toCharArray()) m.merge(c, 1, Integer::sum);\n    for (Map.Entry&lt;Character, Integer&gt; e : m.entrySet())\n        if (e.getValue() == 1) return e.getKey();\n    return null;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LinkedHashMap<\/code> keeps insertion order, so the second loop can go over the map instead of the string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Edge cases<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Empty string, or every character repeats (&#8220;aabb&#8221;): return a clear &#8220;not found&#8221; value and say what it is.<\/li>\n\n\n<li>Case: is &#8220;A&#8221; the same as &#8220;a&#8221;? Ask.<\/li>\n\n\n<li>Spaces and punctuation: count them or skip them?<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Follow-ups<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Return the index instead of the character.<\/li>\n\n\n<li>Do it for a stream of characters, answering after each new character (keep a queue of candidates plus counts).<\/li>\n\n\n<li>Find the first repeating character instead.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>This answer was written by Debugger&#8217;s Space as practice material. It is asked in most Indian IT services interviews (TCS, Infosys, Wipro, HCLTech, Tech Mahindra, LTIMindtree, Accenture, Cognizant, Capgemini, IBM). See each company&#8217;s hiring steps and practice questions on <a href=\"https:\/\/www.debuggersspace.com\/companies\">Debugger&#8217;s Space<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find the first character that appears only once in a string, in two passes with a count array or LinkedHashMap, with complexity and edge cases.<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[263],"tags":[234,236,270,235,275,231,237,229,264,233,288,228,232,230],"class_list":["post-1037","post","type-post","status-publish","format-standard","hentry","category-common-interview-questions","tag-accenture","tag-capgemini","tag-coding-interview-questions","tag-cognizant","tag-freshers","tag-hcltech","tag-ibm","tag-infosys","tag-java-interview-questions","tag-ltimindtree","tag-strings","tag-tcs","tag-tech-mahindra","tag-wipro"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/blog.debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Find the first character that appears only once in a string, in two passes with a count array or LinkedHashMap, with complexity and edge cases.","_links":{"self":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":1,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"predecessor-version":[{"id":1078,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions\/1078"}],"wp:attachment":[{"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}