Largest Number At Least Twice of Others LeetCode solution in JavaScript
1/**2* @param {number[]} nums3* @return {number}4*/5var dominantIndex = function (nums) {6let first = -Infinity;7let second = -Infinity;8let ans = 0;9for (let i = 0; i < nums.length; i++) {10if (nums[i] > first) {11second = first;12first = nums[i];13ans = i;14} else if (nums[i] > second) {15second = nums[i];16}17}18return first >= second * 2 ? ans : -1;19};