- 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
Detecting the first non-repeating string in Array in JavaScript
Suppose, we have an array of strings like this where strings might contain duplicate characters −
const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34'];
We are required to write a JavaScript function that takes in one such array and returns the very first element from the array that contains 0 duplicate characters. If there does not exist any such string, we should return false.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = ['54gdgdfe3', '434ffd', '43frdf', '43fdhnh', 'wgcxhjny', 'fsdf34']; const isUnique = str => { return str.split('').every(el => str.indexOf(el) === str.lastIndexOf(el)); }; const findUniqueString = arr => { for(let i = 0; i < arr.length; i++){ if(isUnique(arr[i])){ return arr[i]; }; }; return false; }; console.log(findUniqueString(arr));
Output
The output in the console will be −
wgcxhjny
- Related Articles
- Detecting the first non-unique element in array in JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Finding first non-repeating character 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
- How to find its first non-repeating character in a given string in android?
- Finding the index of the first repeating character in a string in JavaScript
- First non-repeating in a linked list in C++
- Sum of all the non-repeating elements of an array JavaScript
- JavaScript Find the first non-consecutive number in Array
- Find the last non repeating character in string in C++
- Write a program to find the first non-repeating number in an integer array using Java?
- Finding the first non-consecutive number in an array in JavaScript
- Find the first non-repeating character from a stream of characters in Python

Advertisements