
- 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 longest "uncommon" sequence in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array.
By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
Our function should return the length of this longest uncommon subsequence.
For example: If the input array is −
const arr = ["aba", "cdc", "eae"];
Then the output should be 3.
Example
The code for this will be −
const arr = ["aba", "cdc", "eae"]; const findUncommonLength = (array = []) => { const seen = {}; const arr = []; let max = −1; let index = −1; for(let i = 0; i < array.length; i++){ seen[array[i]] = (seen[array[i]] || 0) + 1; if(seen[array[i]] > 1){ if(max < array[i].length){ max = array[i].length index = i; } } }; if(index === −1) { array.forEach(el =>{ if(el.length > max) max = el.length; }) return max; }; for(let i = 0; i < array.length; i++){ if(seen[array[i]] === 1) arr.push(array[i]); }; max = −1; for(let i = arr.length − 1; i >= 0; i−−){ let l = arr[i]; let d = 0; for(let j = 0; j < array[index].length; j++){ if(array[index][j] === l[d]){ d++; } } if(d === l.length){ let temp = arr[i]; arr[i] = arr[arr.length − 1]; arr[arr.length − 1] = temp; arr.pop(); } }; arr.forEach(el =>{ if(el.length > max) max = el.length; }); return max; }; console.log(findUncommonLength(arr));
Output
And the output in the console will be −
3
- Related Articles
- Finding the longest substring uncommon in array in JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Finding the longest valid parentheses JavaScript
- Finding longest consecutive joins in JavaScript
- Finding and returning uncommon characters between two strings in JavaScript
- Longest Uncommon Subsequence II in C++
- Longest Uncommon Subsequence I in C++
- Finding the longest string in an array in JavaScript
- Finding the longest word in a string in JavaScript
- Finding all the longest strings from an array in JavaScript
- Finding Fibonacci sequence in an array using JavaScript
- Length of the longest possible consecutive sequence of numbers in JavaScript
- Finding nth element of the Padovan sequence using JavaScript
- Finding the nth element of the lucas number sequence in JavaScript
- Finding the missing number in an arithmetic progression sequence in JavaScript

Advertisements