
- 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 unique string in an array in JavaScript
Suppose, we have the following array of strings that 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.
Example
Following is the code −
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
Following is the output in the console −
wgcxhjny
- Related Articles
- Finding the only unique string in an array using JavaScript
- Finding the longest string in an array in JavaScript
- Finding first unique element in sorted array in JavaScript
- Finding the first unique element in a sorted array in JavaScript
- Find unique and biggest string values from an array in JavaScript
- Finding the sum of unique array values - JavaScript
- Mapping unique characters of string to an array - JavaScript
- Constructing array from string unique characters in JavaScript
- Finding unlike number in an array - JavaScript
- Counting unique elements in an array in JavaScript
- Finding all the unique paths in JavaScript
- Finding the rotation of an array in JavaScript
- Finding the mid of an array in JavaScript
- Finding degree of subarray in an array JavaScript
- Finding matching pair from an array in JavaScript

Advertisements