Tuesday, October 30, 2018

Two ways of searching value within an array

The following code demonstrates two ways or methods to know whether a value exists or not within an array :

import java.util.Arrays;

public class SearchingAValueWithinAnArray {
static boolean exists1(int[] ints, int k) {
Arrays.parallelSort(ints);
return Arrays.binarySearch(ints, k) >= 0;
}

static boolean exists2(int[] ints, int k) {
return Arrays.stream(ints).parallel().filter(element -> element == k).findFirst().isPresent();
}

public static void main(String[] args) {
int[] ints = { -9, 14, 37, 102 };
System.out.println(SearchingAValueWithinAnArray.exists1(ints, 102)); // true
System.out.println(SearchingAValueWithinAnArray.exists2(ints, 36)); // false
}
}

1 comment:

  1. Have your account got frozen and all you need help to delete this issue as soon as possible so that you can continue your work with thinking about the worries? This issue not only consumes your time but must be fixed immediately to get access to your account. Whenever you are in such issues and looking for some help, you can always call on Binance support number and get the best guidance from the professionals who enriched with knowledge and have been doing the same experiments on a regular basis from the past years. Take assistance from the team and get out of issues.
    More Info Visit Here: https://www.cryptowalletsupport.com/binance-support-number/

    ReplyDelete