Swift Solution


  • 0
    R
    class Solution {
        func dominantIndex(_ nums: [Int]) -> Int {
            var a = nums
            a.sort()
            if a.count >= 2  && a[a.count-1] >= 2 * a[a.count-2] {
                return find(b:nums, c:a[a.count-1])
            } else if( a.count == 1) {
                return 0
            }
            return -1
        }
        
        func find( b: [Int], c:Int)-> Int {
            for i in 0...b.count {
                if(b[i] == c) {
                    return i
                }
            }
            return -1
        }
    }
    

Log in to reply
 

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