
- 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
JavaScript Program to Find k maximum elements of array in original order
We will be using the JavaScript array sort method and slicing technique to find the k maximum elements of an array in their original order. Firstly, we sort the array in descending order, and then slice it from the beginning to the kth index to obtain the k maximum elements. By preserving the original order of the elements, the significance and context of the data remains intact, making it easier for us to analyze and interpret the results.
Approach
The approach to find k maximum elements of an array in original order can be described as follows −
Create a new empty array to store the k maximum elements.
Loop through the input array and compare each element with the elements in the new array.
If the new array has less than k elements, simply add the current element to the new array.
If the new array has k elements, compare the current element with the minimum element in the new array.
If the current element is greater than the minimum element, remove the minimum element and add the current element to the new array.
Finally, return the new array with the k maximum elements in original order as they appeared in the input array.
Example
Here is a complete working example in JavaScript to find the k maximum elements of an array in their original order −
function findKMaxElements(arr, k) { // Create a copy of the original array let sortedArr = [...arr]; // Sort the copy of the array in descending order sortedArr.sort((a, b) => b - a); // Slice the first k elements of the sorted array let kMaxElements = sortedArr.slice(0, k); // Create a result array to store the k maximum elements in their original order let result = []; // Loop through the original array for (let i = 0; i < arr.length; i++) { // Check if the current element is in the kMaxElements array if (kMaxElements.includes(arr[i])) { // If yes, add it to the result array result.push(arr[i]); // Remove the element from the kMaxElements array // to keep track of the elements that have already been added to the result kMaxElements.splice(kMaxElements.indexOf(arr[i]), 1); // If the kMaxElements array is empty, it means all k maximum elements have been found if (kMaxElements.length === 0) { break; } } } // Return the result array return result; } // Test the function with an example array let arr = [3, 2, 1, 4, 5]; let k = 3; console.log(findKMaxElements(arr, k));
Explanation
The function findKMaxElements takes an array arr and an integer k as input.
The first step is to create a copy of the original array arr and sort it in descending order using the sort method. This will allow us to easily find the k maximum elements.
The slice method is used to extract the first k elements of the sorted array and store them in a new array kMaxElements.
A result array is created to store the k maximum elements in their original order.
A for loop is used to loop through the original array arr. For each element in the array, the code checks if it is in the kMaxElements array using the includes method.
If the current element is in the kMaxElements array, it is added to the result array. The splice method is used to remove the element from the kMaxElements array to keep track of the elements that have already been added to the result.
The loop continues until all k maximum elements have been found and added to the result array, or until the end of the original array is reached.
The final step is to return the result array, which contains the k maximum elements of the original array in their original order.
- Related Articles
- Find k maximum elements of array in original order in C++
- Find array elements that are out of order in JavaScript
- Print n smallest elements from given array in their original order
- C# Program to order array elements in descending order
- C# Program to order array elements
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Program to find maximum product of two distinct elements from an array in Python
- Retrieving n smallest numbers from an array in their original order in JavaScript
- Return the sum of two consecutive elements from the original array in JavaScript
- Maximum sum of n consecutive elements of array in JavaScript
- Find original array from encrypted array (An array of sums of other elements) using C++.
- Maximum value K such that array has at-least K elements that are >= K in C++
- Java Program to Sort the Array Elements in Descending Order
- Maximum sum of increasing order elements from n arrays in C++ program
- JavaScript Program to Find Mth element after K Right Rotations of an Array
