Minimum Moves to Equal Array Elements II LeetCode solution in JavaScript
1var minMoves2 = function (nums) {2// Sort the array low to high3nums.sort(function (a, b) {4return a - b;5});6let i = 0;7let j = nums.length - 1;8let res = 0;9/**10* Sum up the difference between the next highest and lowest numbers. Regardless of what number we wish to move towards, the number of moves is the same.11*/12while (i < j) {13res += nums[j] - nums[i];14i++;15j--;16}17return res;18};