

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unique pairs in array that forms palindrome words in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of unique words.
Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word.
Example
Following is the code −
const arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => { const res = []; for ( let i = 0; i < arr.length; i++ ){ for ( let j = 0; j < arr.length; j++ ){ if (i !== j ) { let k = `${arr[i]}${arr[j]}`; let l = [...k].reverse().join(''); if (k === l) res.push( [i, j] ); } }; }; return res; }; console.log(findPairs(arr));
Output
[ [ 0, 1 ], [ 1, 0 ], [ 2, 4 ], [ 3, 2 ] ]
- Related Questions & Answers
- Counting adjacent pairs of words in JavaScript
- Joining strings to form palindrome pairs in JavaScript
- Palindrome array - JavaScript
- Maximum length product of unique words in JavaScript
- JavaScript function that should count all unique items in an array
- Making array unique in JavaScript
- Count of pairs in an array that have consecutive numbers using JavaScript
- Unique Morse Code Words in Python
- Find the Number of Unique Pairs in an Array using C++
- Count palindrome words in a sentence in C++
- Finding peculiar pairs in array in JavaScript
- Making a Palindrome pair in an array of words (or strings) in C++
- Beginning and end pairs in array - JavaScript
- Find all triplets in a sorted array that forms Geometric Progression in C++
- Filtering array to contain palindrome elements in JavaScript
Advertisements