Added Linear Search function in Java8 (#19214)
This commit is contained in:
committed by
Quincy Larson
parent
fac8e975dd
commit
897535e4a0
@ -105,6 +105,20 @@ func linearSearch(for number: Int, in array: [Int]) -> Int? {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Example in Java
|
||||||
|
```Java 8
|
||||||
|
int linearSearch(int[] arr, int element)
|
||||||
|
{
|
||||||
|
for(int i=0;i<arr.length;i++)
|
||||||
|
{
|
||||||
|
if(arr[i] == element)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
## Global Linear Search
|
## Global Linear Search
|
||||||
|
|
||||||
What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.
|
What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.
|
||||||
|
Reference in New Issue
Block a user