Created
December 3, 2018 16:46
-
-
Save AustinBCole/f5f0cf4bbc9d1c95581c75b52cca2790 to your computer and use it in GitHub Desktop.
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
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