Sunday 26 May 2019

Java: Search for an element in the array


Approach 1: Traversing array using any loop construct and search for the element.

public static boolean isElementExist(String[] data, String element) {
         if (data == null)
                  return false;

         for (String elem : data) {
                  if (elem == null && element == null) {
                           return true;
                  }

                  if (elem != null && element != null && elem.equals(element)) {
                           return true;
                  }
         }

         return false;
}

App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;

public class App {

 public static boolean isElementExist(String[] data, String element) {
  if (data == null)
   return false;

  for (String elem : data) {
   if (elem == null && element == null) {
    return true;
   }

   if (elem != null && element != null && elem.equals(element)) {
    return true;
   }
  }

  return false;
 }

 public static void main(String args[]) throws UnsupportedEncodingException {
  String[] hobbies = { "chess", "cricket", "hockey", "football", "trekking" };

  String myHobby1 = "film making";
  String myHobby2 = "hockey";

  boolean isMyHobby1Exist = isElementExist(hobbies, myHobby1);
  boolean isMyHobby2Exist = isElementExist(hobbies, myHobby2);

  System.out.println("Is " + myHobby1 + " exist : " + isMyHobby1Exist);
  System.out.println("Is " + myHobby2 + " exist : " + isMyHobby2Exist);
 }

}

Output
Is film making exist : false
Is hockey exist : true

Approach 2: Using Arrays.asList method
public static boolean isElementExist(String[] data, String element) {
         if (data == null)
                  return false;

         return Arrays.asList(data).contains(element);
}


App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class App {

 public static boolean isElementExist(String[] data, String element) {
  if (data == null)
   return false;

  return Arrays.asList(data).contains(element);
 }

 public static void main(String args[]) throws UnsupportedEncodingException {
  String[] hobbies = { "chess", "cricket", "hockey", "football", "trekking" };

  String myHobby1 = "film making";
  String myHobby2 = "hockey";

  boolean isMyHobby1Exist = isElementExist(hobbies, myHobby1);
  boolean isMyHobby2Exist = isElementExist(hobbies, myHobby2);

  System.out.println("Is " + myHobby1 + " exist : " + isMyHobby1Exist);
  System.out.println("Is " + myHobby2 + " exist : " + isMyHobby2Exist);
 }

}

Approach 3: Using Arrays.stream method
public static boolean isElementExist(String[] data, String element) {
         if (data == null)
                  return false;

         return  Arrays.stream(data).anyMatch(element::equals);
}


App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class App {

 public static boolean isElementExist(String[] data, String element) {
  if (data == null)
   return false;

  return  Arrays.stream(data).anyMatch(element::equals);
 }

 public static void main(String args[]) throws UnsupportedEncodingException {
  String[] hobbies = { "chess", "cricket", "hockey", "football", "trekking" };

  String myHobby1 = "film making";
  String myHobby2 = "hockey";

  boolean isMyHobby1Exist = isElementExist(hobbies, myHobby1);
  boolean isMyHobby2Exist = isElementExist(hobbies, myHobby2);

  System.out.println("Is " + myHobby1 + " exist : " + isMyHobby1Exist);
  System.out.println("Is " + myHobby2 + " exist : " + isMyHobby2Exist);
 }

}

Approach 4: Using Arrays.binarySearch
public static boolean isElementExist(String[] data, String element) {
         if (data == null)
                  return false;

         Arrays.parallelSort(data);

         return Arrays.binarySearch(data, element) >= 0;

}


App.java
package com.sample.app;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class App {

 public static boolean isElementExist(String[] data, String element) {
  if (data == null)
   return false;

  Arrays.parallelSort(data);

  return Arrays.binarySearch(data, element) >= 0;

 }

 public static void main(String args[]) throws UnsupportedEncodingException {
  String[] hobbies = { "football", "chess", "cricket", "hockey", "trekking" };

  String myHobby1 = "film making";
  String myHobby2 = "hockey";

  boolean isMyHobby1Exist = isElementExist(hobbies, myHobby1);
  boolean isMyHobby2Exist = isElementExist(hobbies, myHobby2);

  System.out.println("Is " + myHobby1 + " exist : " + isMyHobby1Exist);
  System.out.println("Is " + myHobby2 + " exist : " + isMyHobby2Exist);
 }

}


You may like



No comments:

Post a Comment