Simple JavaScript Solution


  • 1
    /**
     * @param {number[]} nums
     * @return {string[]}
     */
    var findRelativeRanks = function(nums) {
      let arr = nums.slice();
      arr.sort((a,b) => b - a);
      let numsMap = {};
      arr.forEach((num, index) => {
        numsMap[num] = (index +1).toString();
        if (index === 0) {
          numsMap[num] = "Gold Medal";
        }
        if (index === 1) {
          numsMap[num] = "Silver Medal";
        }
        if (index === 2) {
           numsMap[num] = "Bronze Medal";
        }
      });
      return nums.map(num => {
        return numsMap[num];
      });
    };

  • 0
    Z

    @ChrisGao Why did you have to slice it?


  • 1

    In order to duplicate the original array, And I need to keep the original array.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.