Pass an int array and an int and do a search of the second parameter in the first, and return true/false.





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:

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

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




Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply