
- 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 only unique string in an array using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of strings. All the strings in the array contain the same characters, or the repetition of characters, and just one string contains a different set of characters. Our function should find and return that string.
For example
If the array is −
[‘ba’, 'abc', 'acb', 'bac', 'foo', 'bca', 'cab', 'cba' ]
Then the required string is ‘foo’.
Strings may contain spaces. Spaces are not significant, only non-spaces symbols matter. Example, a string that contains only spaces is like an empty string. It’s guaranteed that the array contains more than 3 strings.
Example
Following is the code −
const arr = ['ba', 'abc', 'acb', 'bac', 'foo', 'bca', 'cab', 'cba' ]; const findOnlyUnique = (arr = []) => { const first = []; for(i = 0; i < arr.length; i++){ first.push(arr[i].toLowerCase().replace(/\s/g, '').split('')); for (j = 0; j < arr[i].length; j++){ first[i].sort(); } } const second = []; for (k = 0; k < arr.length; k++){ second.push(first[k].join()); } second.sort(); const third = []; if (second[1] !== second[second.length - 1]) { third.push(second[second.length - 1]); }else{ third.push(second[0]); } const last = []; for(let n = 0; n < first.length; n++){ last.push(first[n].join(',')); } return (arr[last.indexOf(third[0])]); }; console.log(findOnlyUnique(arr));
Output
foo
- Related Articles
- Finding unique string in an array in JavaScript
- Finding the only out of sequence number from an array using JavaScript
- Finding the longest string in an array in JavaScript
- Finding the sum of unique array values - JavaScript
- Finding first unique element in sorted array in JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- Finding the first unique element in a sorted array in JavaScript
- Mapping unique characters of string to an array - JavaScript
- Finding product of an array using recursion in JavaScript
- Find unique and biggest string values from an array in JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Finding all the unique paths in JavaScript
- Constructing array from string unique characters in JavaScript
- Finding the rotation of an array in JavaScript
- Finding the mid of an array in JavaScript

Advertisements