Skip to content

Instantly share code, notes, and snippets.

@leodabus
Created January 27, 2017 03:18
Show Gist options
  • Save leodabus/8750df9fd13d88f129bbe71434b87637 to your computer and use it in GitHub Desktop.
Save leodabus/8750df9fd13d88f129bbe71434b87637 to your computer and use it in GitHub Desktop.
extension CGPoint: Hashable {
public var hashValue: Int {
return (self as NSValue).hashValue
}
}
@leodabus
Copy link
Author

let cgPoints = [CGPoint(x: 532.7, y: 150.0), CGPoint(x: 66.6, y: 150.0), CGPoint(x: 129.2, y: 150.0), CGPoint(x: 129.2, y: 150.0), CGPoint(x: 301.2, y: 150.0), CGPoint(x: 444.2, y: 150.0), CGPoint(x: 532.7, y: 150.0), CGPoint(x: 532.7, y: 150.0), CGPoint(x: 532.7, y: 150.0)]

var cgPointOccurrences: [CGPoint: Int] = [:]

cgPoints.forEach{
    cgPointOccurrences[$0] = (cgPointOccurrences[$0] ?? 0) + 1
}

if let maximumOcurrences = cgPointOccurrences.max(by: {$0.value < $1.value}) {
    print("\(maximumOcurrences.key) occurred \(maximumOcurrences.value) times")  // "(532.7, 150.0) occurred 4 times\n"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment