Created
June 30, 2020 05:44
-
-
Save alexnikol/32ca2d8f323d612f281ddf331b898368 to your computer and use it in GitHub Desktop.
PickingNumbers HackerRank - Solving
This file contains 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
func pickingNumbers(a: [Int]) -> Int { | |
var repeatList = Array(repeating: 0, count: 100) //Generated list with 100 zeros | |
for i in 0..<a.count { //Count repeating of each item in input array | |
repeatList[a[i] - 1] += 1 | |
} | |
var result = 0 | |
for i in 0..<(repeatList.count - 1) { | |
let stepSum = repeatList[i] + repeatList[i + 1] //Sum of each item and next item | |
if stepSum > result { //The biggest met sum is our answer | |
result = stepSum | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment