Skip to content

Instantly share code, notes, and snippets.

@leojkwan
Last active April 20, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leojkwan/d65b7401bc321aceb617e0ccf93105df to your computer and use it in GitHub Desktop.
Save leojkwan/d65b7401bc321aceb617e0ccf93105df to your computer and use it in GitHub Desktop.
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