Skip to content

Instantly share code, notes, and snippets.

@piyushdec
Created July 12, 2019 21:19
Show Gist options
  • Select an option

  • Save piyushdec/254a373bf94e5472df274f1198be5425 to your computer and use it in GitHub Desktop.

Select an option

Save piyushdec/254a373bf94e5472df274f1198be5425 to your computer and use it in GitHub Desktop.
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