Leetcode Note: Go - N Repeated Element in Size 2N Array

N-Repeated Element in Size 2N Array - LeetCode
https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

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

回答

N-Repeated Element in Size 2N Array - LeetCode
https://leetcode.com/problems/n-repeated-element-in-size-2n-array/solution/

Go 94% O(N) time O(1) space solution - LeetCode Discuss
https://leetcode.com/problems/n-repeated-element-in-size-2n-array/discuss/493239/Go-94-O(N)-time-O(1)-space-solution

func repeatedNTimes(A []int) int {
    for i := 0; i < len(A); i += 2 {
        if A[i] == A[i+1] {
            return A[i]
        }
    }

    if A[0] == A[2] || A[0] == A[3] {
        return A[0]
    }

    return A[1]
}