
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the first non-repeating character of a string in JavaScript
We are required to write a JavaScript function that takes in a string as the first and the only argument.
The function should find and return the index of first character it encounters in the string which appears only once in the string.
If the string does not contain any unique character, the function should return -1.
For example −
If the input string is −
const str = 'hellohe';
Then the output should be −
const output = 4;
Example
Following is the code −
const str = 'hellohe'; const firstUnique = (str = '') => { let obj = {}; for(let i = 0; i < str.length; i++){ if(str[i] in obj){ let temp = obj[str[i]]; let x = parseInt(temp[0]); x += 1; temp[0] = x; obj[str[i]] = temp; } else { obj[str[i]] = [1, i] } } let arr = Object.keys(obj); for(let i = 0; i < arr.length; i++){ let z = obj[arr[i]] if(z[0] === 1){ return z[1]; } } return -1; }; console.log(firstUnique(str));
Output
Following is the console output −
4
- Related Articles
- Finding first non-repeating character JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- Return index of first repeating character in a string - JavaScript
- First non-repeating character using one traversal of string in C++
- Detecting the first non-repeating string in Array in JavaScript
- How to find its first non-repeating character in a given string in android?
- Find the first non-repeating character from a stream of characters in Python
- Find first repeating character using JavaScript
- Find the last non repeating character in string in C++
- Python program to Find the first non-repeating character from a stream of characters?
- Java program to Find the first non-repeating character from a stream of characters
- Finding the largest non-repeating number in an array in JavaScript
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- First non-repeating in a linked list in C++
- Finding the first non-consecutive number in an array in JavaScript

Advertisements