描述
Given an unsorted array of n elements, find if the element k is present in the array or not.
Complete the findnNumber function in the editor below.It has 2 parameters:
1 An array of integers, arr, denoting the elements in the array.
2 An integer, k, denoting the element to be searched in the array.
The function must return true or false denoting if the element is present in the array or not
说明
- 1 <= n <= 10^5
- 1 <= arr[i] <= 10^9
样例
- Example:
Input:
arr = [1, 2, 3, 4, 5]
k = 1
Output: true
解析
public class Solution {
public boolean findnNumber(int[] nums, int k) {
for (int num : nums) {
if (num == k) {
return true;
}
}
return false;
}
}