java

Check Element in Array()

Parameters: elementToCheck(Object), array(Object[])

The element to check for and the array to search in.

Returns: Returns 'true' if element is found, 'false' otherwise.

Using this function, one can easily check if an array contains a certain element or not in Java. This is a commonly used approach when dealing with arrays.

variables
arrays
conditionals
loops
Easy dificulty

How to Check an Element in an Array with Java

Welcome to this programmer-friendly blog post. Today, we are going to briefly explain how to design a function in Java for checking an element within an array. The coming steps will simplify this process, making it easy to grasp. This well-crafted tutorial is devoid of dialects or slang to cater for every reader across the globe. Enjoy your coding journey!

Step 1: Define the Function

First, we need to define a function that accepts two arguments: an array and an element that we will search for within this array. Let's call this function 'checkElementInArray'.

public static boolean checkElementInArray(int[] array, int element) {

}

Step 2: Implement the Search Logic

Now, let's implement the function by using a simple 'for-each' loop to iterate over each element in the array. If an array element matches the target element, the function will return 'true'.

public static boolean checkElementInArray(int[] array, int element) {
  for (int e : array) {
    if (e == element) {
      return true;
    }
  }
}

Step 3: Return False by Default

If the array does not contain the element, the function will return 'false'. This will be the default return value because it is outside of the for loop.

public static boolean checkElementInArray(int[] array, int element) {
  for (int e : array) {
    if (e == element) {
      return true;
    }
  }
  return false;
}

Step 4: Test the Function

Now, let's test our function using an array and an element. Let's say the array is 5 and the element is 3. It should return true because 3 exists in the array.

public static void main(String[] args) {
  int[] testArray = {1, 2, 3, 4, 5};
  System.out.println(checkElementInArray(testArray, 3)); // This will print: true
}

Conclusion

In conclusion, our function 'checkElementInArray' effectively checks whether a given element exists in a given array, returning 'true' if it does and 'false' if it does not. This kind of function can be very useful for various tasks that involve searching for items in collections.

public class Main {
  public static void main(String[] args) {
    int[] testArray = {1, 2, 3, 4, 5};
    System.out.println(checkElementInArray(testArray, 3)); // This will print: true
  }

  public static boolean checkElementInArray(int[] array, int element) {
    for (int e : array) {
      if (e == element) {
        return true;
      }
    }
    return false;
  }
}

Learn function in:

Array Element Check

Checks if a particular element is present in an array.

Learn more

Mathematical principle

The function utilizes the principle of Linear Search. Linear search is a very simple search algorithm where we start from one end of the list and check every element until we find the target or exhaust the list. For an array with `n` elements, we have to check `n` elements in the worst case.

Learn more