> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/array/747.-largest-number-at-least-twice-of-others.md).

# # 747. Largest Number At Least Twice of Others

[leetcode](https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/)

<pre class="language-javascript"><code class="lang-javascript"><strong>/*
</strong><strong>Input: nums = [3,6,1,0]
</strong>Output: 1

Input: nums = [1,2,3,4]
Output: -1
*/

/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function(nums) {
    let maxValueIndex = 0
    let doubleNums = [];
    for(let i=0; i&#x3C;nums.length; i++){
        doubleNums[i] = nums[i]*2;
        maxValueIndex = nums[maxValueIndex] > nums[i] ? maxValueIndex : i
    }
    
     for(let i=0; i&#x3C;doubleNums.length; i++){
        if(i!== maxValueIndex &#x26;&#x26; doubleNums[i] > nums[maxValueIndex]){
            return -1
        }
     }
    
    return maxValueIndex
    
};
</code></pre>

### 怎樣可以寫更好

我本來是陣列所有 `item*2` ，然後再逐一比較有沒有大於最大值但這樣要 for 兩次，其實可以找出最大跟第二大就好，若 `second*2 > first 那就 return -1` ，一個 for 就可以搞定

```javascript
/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function(nums) {
    let index = 0
    let first = nums[index]
    let second = -Infinity;
    
    for(let i=1; i< nums.length; i++){
        if(nums[i] > first){
            second = first
            first = nums[i]
            index = i
        } else if(nums[i] > second && nums[i] < first) {
            second = nums[i]
        }
    }
    return first >= second*2 ? index : -1
    
};
```

8/13

```javascript
var dominantIndex = function(nums) {
    let max = nums[0];
    let secondMax = -Infinity
    let maxIndex = 0

    for(let i=1; i<nums.length; i++){
        if(nums[i] > max){
            secondMax = max
            max = nums[i]
            maxIndex = i
        } else {
            secondMax = Math.max(secondMax, nums[i])
        }
        
    }
    return max >= secondMax*2 ? maxIndex : -1
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hannahpun.gitbook.io/leetcode-note/array/747.-largest-number-at-least-twice-of-others.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
