Added Linear Search function in Java8 (#19214)

This commit is contained in:
Devansh Upadhyay
2018-10-16 09:44:59 +05:30
committed by Quincy Larson
parent fac8e975dd
commit 897535e4a0

View File

@ -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 5s are in an array. What if you are searching the multiple occurrences of an element? For example you want to see how many 5s are in an array.