Created
July 12, 2019 21:19
-
-
Save piyushdec/254a373bf94e5472df274f1198be5425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Check if A[i] == A[i - 1] or A[i] == A[i - 2] | |
| If so, we return A[i] | |
| If not, it must be [x, x, y, z] or [x, y, z, x]. | |
| We return A[0] for the cases that we miss. | |
| O(N) time O(1) space | |
| class Solution { | |
| func repeatedNTimesIII(_ A: [Int]) -> Int { | |
| for i in 2..<A.count { | |
| if A[i] == A[i-1] || A[i] == A[i-2] { | |
| return A[i] | |
| } | |
| } | |
| return A[0] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment