Last active
April 20, 2016 22:01
-
-
Save leojkwan/d65b7401bc321aceb617e0ccf93105df 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
import Cocoa | |
// MARK: Goal- Find the number of times any item appears in array | |
var tayLyrics = "Nice to meet you, where you been? I could show you incredible things Magic, madness, heaven, sin Saw you there and I thought Oh my God, look at that face You look like my next mistake Love’s a game, want to play? New money, suit & tie" | |
var stringArray = tayLyrics.componentsSeparatedByString(" ") | |
var intArray = [1,4,5,2,6,6,6] | |
// This method simply means | |
func findItemCountForArray<T:Hashable>(arr:[T]) -> [T: Int] { | |
// 1 | |
var countDict = [T: Int]() | |
for key in arr { | |
// 2 | |
if countDict[key] == nil { | |
countDict[key] = 0 | |
} | |
// 3 | |
countDict[key]! += 1 | |
} | |
return countDict | |
} | |
findItemCountForArray(stringArray) | |
findItemCountForArray(intArray) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment