# andyRon/swift-algorithm-club-cn

Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.

# 统计出现次数(Count Occurrences)

``````[ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
``````

``````[ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
*  *  *  *
``````

```func countOccurrencesOfKey(_ key: Int, inArray a: [Int]) -> Int {
func leftBoundary() -> Int {
var low = 0
var high = a.count
while low < high {
let midIndex = low + (high - low)/2
if a[midIndex] < key {
low = midIndex + 1
} else {
high = midIndex
}
}
return low
}

func rightBoundary() -> Int {
var low = 0
var high = a.count
while low < high {
let midIndex = low + (high - low)/2
if a[midIndex] > key {
high = midIndex
} else {
low = midIndex + 1
}
}
return low
}

return rightBoundary() - leftBoundary()
}```

```let a = [ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]

countOccurrencesOfKey(3, inArray: a)  // returns 4```

``````[ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
``````

``````[ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
*
``````

``````[ 0, 1, 1, 3, 3, 3 | x, x, x, x, x, x ]
*
``````

``````[ 0, 1, 1 | x, x, x | x, x, x, x, x, x ]
*
``````

``````[ x, x | 1 | x, x, x | x, x, x, x, x, x ]
*
``````

``````[ 0, 1, 1, 3, 3, 3, 3, 6, 8, 10, 11, 11 ]
*

[ x, x, x, x, x, x, x | 6, 8, 10, 11, 11 ]
*

[ x, x, x, x, x, x, x | 6, 8, | x, x, x ]
*

[ x, x, x, x, x, x, x | 6 | x | x, x, x ]
*
``````