
- 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
Common element with least index sum in JavaScript
Problem
We are required to write a JavaScript function that takes in two arrays of literals, arr1 and arr2, as the first and the second argument.
Our function should find out the common element in arr1 and arr2 with the least list index sum. If there is a choice tie between answers, we should output all of them with no order requirement.
For example, if the input to the function is−
const arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['d', 'a', 'c'];
Then the output should be −
const output = ['a'];
Output Explanation
Since 'd' and 'a' are common in both the arrays the index sum of 'd' is 3 + 0 = 3 and that of 'a' is 0 + 1 = 1, hence 'a' is the required element.
Example
Following is the code −
const arr1 = ['a', 'b', 'c', 'd']; const arr2 = ['d', 'a', 'c']; const findCommon = (arr1 = [], arr2 = []) => { let sum = Infinity const map = arr1.reduce((acc, str, index) => { acc[str] = index return acc }, {}) for (let i = 0; i < arr2.length; i++) { const index1 = map[arr2[i]] if (index1 >= 0 && index1 + i < sum) { sum = index1 + i } } const result = [] for (let i = 0; i < arr2.length; i++) { const index1 = map[arr2[i]] if (index1 >= 0 && index1 + i === sum) { result.push(arr2[i]) } } return result } console.log(findCommon(arr1, arr2));
Output
Following is the console output−
['a']
- Related Articles
- Subarray sum with at least two elements in JavaScript
- Even index sum in JavaScript
- Calculating least common of a range JavaScript
- Index of closest element in JavaScript
- Cumulative sum at each index in JavaScript
- Minimum Index Sum for Common Elements of Two Lists in C++
- Reverse index value sum of array in JavaScript
- Greatest sum and smallest index difference in JavaScript
- JavaScript Program for the Least Frequent Element in an Array
- Function to calculate the least common multiple of two numbers in JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?
- Checking if decimals share at least two common 1 bits in JavaScript
- Sum excluding one element in JavaScript
- Shortest Subarray with Sum at Least K in C++
- Python program to check if two lists have at least one common element

Advertisements