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.

Updated on: 15-Mar-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements