Added example for linear search in Swift

This commit is contained in:
Marwan Alani
2018-10-11 23:23:05 -04:00
committed by Kristofer Koishigawa
parent 9936a89343
commit 231b339755

View File

@ -95,6 +95,16 @@ def linear_search(array, num):
return -1
```
### Example in Swift
```swift
func linearSearch(for number: Int, in array: [Int]) -> Int? {
for (index, value) in array.enumerated() {
if value == number { return index } // return the index of the number
}
return nil // the number was not found in the array
}
```
## Global Linear Search
What if you are searching the multiple occurrences of an element? For example you want to see how many 5s are in an array.