
- 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
Largest difference between element with a twist in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The function should find the difference between the largest and the smallest element of the array.
The condition is that the element which is smaller should appear before the greater element in the original array.
For example −
Consider the following array of Numbers −
const arr = [2, 5, 6, 12, 1];
For this array, our function should output 10.
Although the greatest and the smallest elements of the array are 12 and 1 respectively, since 1 does not appear before 12, we cannot consider it a valid smaller number for the purpose of this question.
Therefore, our function returns the difference −
12 - 2 = 10
Example
Following is the code −
const arr = [2, 5, 6, 12, 1]; const findLargestDifference = (arr = []) => { if (arr.length <= 1){ return -1; }; let min = arr[0]; let diff = 0; for (let i = 1; i < arr.length; i++) { if (arr[i] > min && (arr[i] - min > diff)) { diff = arr[i] - min; } else if (arr[i] <= min) { min = arr[i]; } } if (diff <= 0){ return -1 }; return diff; }; console.log(findLargestDifference(arr));
Output
Following is the output on console −
10
- Related Articles
- Reversing strings with a twist in JavaScript
- Consecutive ones with a twist in JavaScript
- Find difference between first and second largest array element in Java
- Largest index difference with an increasing value in JavaScript
- Paul Smith: “Classic with a Twist”
- Element with largest frequency in list in Python
- Largest Substring Between Two Equal Characters in a string in JavaScript
- How to find the second largest element in a user-input JavaScript array?
- Return the largest array between arrays JavaScript
- Detecting the largest element in an array of Numbers (nested) in JavaScript
- Kth Largest Element in a Stream in Python
- Difference between element and compound.
- Kth Largest Element in an Array
- Maximum difference between a number JavaScript
- What is the difference between break with a label and without a label in JavaScript?

Advertisements