Skip to content

Commit 8a7db67

Browse files
authored
Update README.markdown
1 parent 809cb97 commit 8a7db67

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Count Occurrences/README.markdown

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ The trick is to use two binary searches, one to find where the `3`s start (the l
2222
In code this looks as follows:
2323

2424
```swift
25-
func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
25+
func countOccurrences<T: Comparable>(of key: T, in a: [T]) -> Int {
2626
var leftBoundary: Int {
2727
var low = 0
28-
var high = array.count
28+
var high = a.count
2929
while low < high {
3030
let midIndex = low + (high - low)/2
31-
if array[midIndex] < key {
31+
if a[midIndex] < key {
3232
low = midIndex + 1
3333
} else {
3434
high = midIndex
@@ -39,10 +39,10 @@ func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
3939

4040
var rightBoundary: Int {
4141
var low = 0
42-
var high = array.count
42+
var high = a.count
4343
while low < high {
4444
let midIndex = low + (high - low)/2
45-
if array[midIndex] > key {
45+
if a[midIndex] > key {
4646
high = midIndex
4747
} else {
4848
low = midIndex + 1
@@ -55,7 +55,9 @@ func countOccurrences<T: Comparable>(of key: T, in array: [T]) -> Int {
5555
}
5656
```
5757

58-
Notice that the variables `leftBoundary` and `rightBoundary` are very similar to the [binary search](../Binary%20Search/) algorithm. The big difference is that they don't stop when they find the search key, but keep going. Also, notice that we constrain the type `T` to be Comparable so that the algorithm can be applied to an array of Strings, Ints or other types that conform to the Swift Comparable protocol.
58+
Notice that the variables `leftBoundary` and `rightBoundary` are very similar to the [binary search](../Binary%20Search/) algorithm. The big difference is that they don't stop when they find the search key, but keep going. Also, notice that we constrain the type `T` to be Comparable so that the algorithm can be applied to an
59+
60+
y of Strings, Ints or other types that conform to the Swift Comparable protocol.
5961

6062
To test this algorithm, copy the code to a playground and then do:
6163

0 commit comments

Comments
 (0)