
- 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
Longest subarray with unit difference in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument
Our function should find and return the length of such a subarray where the difference between its maximum value and its minimum value is exactly 1.
For example, if the input to the function is −
const arr = [2, 4, 3, 3, 6, 3, 4, 8];
Then the output should be −
const output = 5;
Output Explanation
Because the desired subarray is [4, 3, 3, 3, 4]
Example
Following is the code −
const arr = [2, 4, 3, 3, 6, 3, 4, 8]; const longestSequence = (arr = []) => { const map = arr.reduce((acc, num) => { acc[num] = (acc[num] || 0) + 1 return acc }, {}) return Object.keys(map).reduce((max, key) => { const nextKey = parseInt(key, 10) + 1 if (map[nextKey] >= 0) { return Math.max( max, map[key] + map[nextKey], ) } return max }, 0); }; console.log(longestSequence(arr));
Output
Following is the console output −
5
- Related Articles
- Longest subarray with absolute difference equal to some number in JavaScript
- Longest decreasing subsequence subarray in JavaScript
- Longest Subarray with GCD Greater than 1
- Longest Turbulent Subarray in C++
- Longest subarray which only contains strictly increasing numbers JavaScript
- Subarray with the greatest product in JavaScript
- Subarray pairs with equal sums in JavaScript
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit in C++
- Contiguous subarray with 0 and 1 in JavaScript
- Subarray sum with at least two elements in JavaScript
- Longest string with two distinct characters in JavaScript
- Golang program to find longest subarray with a given sum using two-pointer approach
- JavaScript Program to Find the subarray with least average
- Check for Subarray in the original array with 0 sum JavaScript
- Unit Testing Challenges with Modular JavaScript Patterns

Advertisements