Test cases
Test cases | Expected | Result |
---|---|---|
searchElement({33,44,21,98,27,94},21) | true | OK |
searchElement({1232,432,546,980,984},980) | true | OK |
searchElement({3,4,9,87,29},2) | false | OK |
Java solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Program9 { public static void main(String[] args) { int a[] = {33,44,21,98,27,94}; int b[] = {1232,432,546,980,984}; int c[] = {3,4,9,87,29}; Program9.searchElement(a, 21); Program9.searchElement(b, 980); Program9.searchElement(c, 2); } public static boolean searchElement(int[] a, int numberToSearch){ for (int i = 0; i < a.length; i++) { if (a[i] == numberToSearch) { return true; } } return false; } } |
Javascript solution
1 2 3 4 5 6 7 8 9 10 11 12 |
const searchElement = (array, number) => { for (let index = 0; index < array.length; index++) { if (array[index] === number) { return true; } } return false; } console.log(searchElement([33,44,21,98,27,94], 21)); //true console.log(searchElement([1232,432,546,980,984], 980)); //true console.log(searchElement([3,4,9,87,29], 2)); //false |