Find First Unique Character in a String

You are given a string S of length N. Your task is to find the index (considering 1-based indexing) of the first unique character in the string. If there are no unique characters, return -1.

Example:

Input:
 T = 2 
N = 5
S = "hello"
N = 4
S = "aabb"
Output:
 1 
-1
Explanation:

The first unique character in "hello" is 'h', which is at index 1. The string "aabb" has no unique characters.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 10^4
Note:

A unique character in a string is one that appears only once.

AnswerBot
4mo

Find the index of the first unique character in a given string, return -1 if no unique characters are found.

  • Iterate through the string to count the frequency of each character

  • Iterate through the strin...read more

Help your peers!
Select
Add answer anonymously...
Quest Technologies Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits