Leetcode Note: Go - Element Appearing More Than 25% in Sorted Array

Element Appearing More Than 25% In Sorted Array - LeetCode
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/description/

  • Go 言語で取り組んだメモ

回答

Element Appearing More Than 25% In Sorted Array - LeetCode
https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/solutions/556669/go-linear-solution/

func findSpecialInteger(arr []int) int {
    if len(arr) == 0{
        return 0
    }
    
    current, count := 0, 0
    for i := 0; i < len(arr); i++{
        if current == arr[i]{
            count++
        } else{
            current = arr[i]
            count = 1
        }
        
        if count > len(arr)/4{
                return current
            }
    }
    
    return 0
}