skip to Main Content

the leet code question states,

You are given an array of unique integers salary where salary[i] is the salary of the ith employee.
Return the average salary of employees excluding the minimum and maximum salary. Answers within 10-5 of the actual answer will be accepted.

i expect to start off the first indices with let sal = salary[0], i wanted to compare the other salaries to salary[0] and if it was less, i wanted to push the lower salaries into a new array. however im not sure how to seperate the lowest salary or finish the problem

/**
* @param {number[]} salary
* @return {number}
*/
var average = function(salary) {
    let sal = salary[0]
    for(let i = 1; i < salary.length; i++){
        if (sal > salary[i]) {
            salary.map()
        }
    }
 

};

4

Answers


  1. For that I would sort the array, and then remove the first and last elements, since the first one will be the lowest salary, and the last one will be the highest salary. Then, with the trimmed array, I’d do the average:

    var average = (salary) => {
        const trimmedArray = salary.sort().slice(1, -1);
    
        const sum = trimmedArray.reduce((prev, curr) => prev + curr)
    
        return sum / trimmedArray.length;
    }
    
    Login or Signup to reply.
  2. 
    const average = (salaries) => {
        if(salaries.length <= 2) return 0;
        // This could be done in one pass, but we do it like this for the sake of convenience
        // The max/min could technically be found in one iteration but the JS Math library does not support it,
        // it would be rather trivial to implement one if this became a performance issue, but it likely wont
        const max = Math.max(...salaries);
        const min = Math.min(...salaries);
    
    
        // take advantage of the fact that all salaries are unique, so we can do direct comparisons.
        const net = salaries.reduce((total, s) => s != min && s != max ? total + s : total , 0);
        return net / (salaries.length - 2);
    }
    
    
    Login or Signup to reply.
  3. you can first sort the array then do what ever you want

    var average = function (salary) {
        let sortedArr = salary.sort()
        let avg = 0;
        //start from 1 to ignore the first element(smallest salary) and end in salary.length-1 to ignore the last element(biggest salary)
        for (let i = 1; i < salary.length - 1; i++) {
            avg += sortedArr[i]
        }
        return avg / (salary.length - 2);
    
        // you can get the smallest salary by sortedArr[0]
        // you can get the biggest salary by sortedArr[sortedArr.length - 1]
    };
    
    Login or Signup to reply.
  4.     const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        
    var average = function(salary) { 
        const max = Math.max(...salary)
        const min = Math.min(...salary)
            
        const maxIndex = salary.indexOf(max)
        const minIndex = salary.indexOf(min)
            
        salary.splice(maxIndex, 1); // Remove one item at index maxIndex
        salary.splice(minIndex, 1); // Remove one item at index minIndex
            
        const sum = salary.reduce((pre,curr)=>pre+curr,0) 
        const elements = salary.length
        return sum/elements
    };
    console.log(average(array)) // 5.5
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search