- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding first non-repeating character JavaScript
We have an array of Numbers/String literals where most of the entries are repeated. Our job is to write a function that takes in this array and returns the index of first such element which does not make consecutive appearances.
If there are no such elements in the array, our function should return -1. So now, let's write the code for this function. We will use a simple loop to iterate over the array and return where we find non-repeating characters, if we find no such characters, we return -1 −
Example
const arr = ['d', 'd', 'e', 'e', 'e', 'k', 'j', 'j', 'h']; const firstNonRepeating = arr => { let count = 0; for(let ind = 0; ind < arr.length-1; ind++){ if(arr[ind] !== arr[ind+1]){ if(!count){ return ind; }; count = 0; } else { count++; } }; return -1; }; console.log(firstNonRepeating(arr));
Output
The output in the console will be −
5
- Related Articles
- Finding the first non-repeating character of a string in JavaScript
- Finding the index of the first repeating character in a string in JavaScript
- Find first repeating character using JavaScript
- First non-repeating character using one traversal of string in C++
- Finding the largest non-repeating number in an array in JavaScript
- Return index of first repeating character in a string - JavaScript
- Find the first non-repeating character from a stream of characters in Python
- Detecting the first non-repeating string in Array in JavaScript
- How to find its first non-repeating character in a given string in android?
- 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
- Find the last non repeating character in string in C++
- Finding the first non-consecutive number in an array in JavaScript
- First non-repeating in a linked list in C++
- Finding length of repeating decimal part in JavaScript

Advertisements