 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return the index of first character that appears twice in a string in JavaScript
We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.
If there is no such character then we should return -1.
Example
The code for this will be −
const str = 'Hello world, how are you'; const firstRepeating = str => {    const map = new Map();    for(let i = 0; i < str.length; i++){       if(map.has(str[i])){          return map.get(str[i]);       };       map.set(str[i], i);    };    return -1; }; console.log(firstRepeating(str));  Output
Following is the output on console −
2
Advertisements
 