admin管理员组

文章数量:1290952

I want JavaScript function, that receive an array as input and output the indices of the array in a order which if value is max it's index should be at [0] of new array, second max [1] and so on ...

for example: [3, 4, 5, 1, 2] as input should give [2,1,0,4,3] output

I code this function but I want a way with one for loop or one map function

const maxValueIndexes = (array) => {

    if (array.length === 0) return []; 
    
    const indexedArray = array.map((value, index) => ({value, index }));
    indexedArray.sort((a, b) => b.value - a.value);
    return indexedArray.map((item) => item.index);
}

Is there any better way in terms of time complexity ??

for example O(n) answer

if there is solution please write it for me

I want JavaScript function, that receive an array as input and output the indices of the array in a order which if value is max it's index should be at [0] of new array, second max [1] and so on ...

for example: [3, 4, 5, 1, 2] as input should give [2,1,0,4,3] output

I code this function but I want a way with one for loop or one map function

const maxValueIndexes = (array) => {

    if (array.length === 0) return []; 
    
    const indexedArray = array.map((value, index) => ({value, index }));
    indexedArray.sort((a, b) => b.value - a.value);
    return indexedArray.map((item) => item.index);
}

Is there any better way in terms of time complexity ??

for example O(n) answer

if there is solution please write it for me

Share Improve this question edited 22 hours ago Areza-s1011 asked 23 hours ago Areza-s1011Areza-s1011 155 bronze badges 3
  • 1 Stack Overflow is a question and answer platform for specific questions. Do you have a specific question? "I want..." or "Write the code for me" aren't questions. – jabaa Commented 23 hours ago
  • 1 thank you for your comment, will try to fix it – Areza-s1011 Commented 22 hours ago
  • 3 "Is there any better way in terms of time complexity ??" No, probably not. .sort has a time complexity of at least O(n log n). The additional loops don't change anything. They have a time complexity of O(n). O(n log n + n) == O(n log n) + O(n) == O(n log n) – jabaa Commented 22 hours ago
Add a comment  | 

2 Answers 2

Reset to default 1

You could get the keys of the array and sort according of value.

(And yes, an interator is a loop.)

const
    array = [3, 4, 5, 1, 2],
    result = [...array.keys()].sort((i, j) => array[j] - array[i]);

console.log(result); // [2, 1, 0, 4, 3]

If you want to achieve this with only one map, you can do it by maintaining a list of indices and sorting them based on the corresponding values in the array. Here's how you can do it with a single map function:

function getSortedIndices(arr) {
  return arr
    .map((_, index) => index) // Create an array of indices [0, 1, 2, ...]
    .sort((a, b) => arr[b] - arr[a]); // Sort indices based on the values in the original array
}

// Example usage:
const input = [3, 4, 5, 1, 2];
const output = getSortedIndices(input);
console.log(output); // Output: [2, 1, 0, 4, 3]

本文标签: