Skip to content

Instantly share code, notes, and snippets.

@AustinBCole
Created December 3, 2018 16:46
Show Gist options
  • Save AustinBCole/f5f0cf4bbc9d1c95581c75b52cca2790 to your computer and use it in GitHub Desktop.
Save AustinBCole/f5f0cf4bbc9d1c95581c75b52cca2790 to your computer and use it in GitHub Desktop.
Assumptions:
I don't have any assumptions. The instructions were pretty straightforward.
Test Cases:
[0, 1, 2, 3]
[2, 2, 3, 3, 2]
[]
Implementation:
First I will guard against an empty array. The function will return nil if the array is empty.The I will loop through the
with a for loop, hopefully use a built-in method to check for number of elements for each index, and then use a modulo to see
if it is divisible by 0. If it is then I will return the first index found with an even amount of occurences.
Code:
import UIKit
func evenOccurence(array: [Int]) -> Int? {
if array == [] {
return nil
}
var count = 1
var otherArray = array
otherArray.removeFirst()
var newArray: [Int] = [otherArray[0]]
newArray = [otherArray[0]]
for index in array {
if newArray.contains(index) && otherArray.count > 0 {
newArray += [index]
otherArray.removeFirst()
count += 1
}
}
print(newArray)
return count
}
evenOccurence(array: [8,9,9, 8, 9, 8, 9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment