How to Check if an Array Contains a Value in Java Efficiently?

How to check if an array (unsorted) contains a certain value? This is a very useful and frequently used operation in Java. It is also a top voted question on Stack Overflow. As shown in top voted answers, this can be done in several different ways, but the time complexity could be very different. In the following I will show the time cost of each method.

1. Four Different Ways to Check If an Array Contains a Value

1) Using List:

public static boolean useList(String[] arr, String targetValue) {
	return Arrays.asList(arr).contains(targetValue);

2) Using Set:

public static boolean useSet(String[] arr, String targetValue) {
	Set<String> set = new HashSet<String>(Arrays.asList(arr));
	return set.contains(targetValue);

3) Using a simple loop:

public static boolean useLoop(String[] arr, String targetValue) {
	for(String s: arr){
			return true;
	return false;

4) Using Arrays.binarySearch():
binarySearch() can ONLY be used on sorted arrays. If the array is sorted, you can use the following code to search the target element:

public static boolean useArraysBinarySearch(String[] arr, String targetValue) {	
	int a =  Arrays.binarySearch(arr, targetValue);
	if(a > 0)
		return true;
		return false;

2. Time Complexity

The approximate time cost can be measured by using the following code. The basic idea is to search an array of size 5, 1k, 10k. The approach may not be precise, but the idea is clear and simple.

public static void main(String[] args) {
	String[] arr = new String[] {  "CD",  "BC", "EF", "DE", "AB"};
	//use list
	long startTime = System.nanoTime();
	for (int i = 0; i < 100000; i++) {
		useList(arr, "A");
	long endTime = System.nanoTime();
	long duration = endTime - startTime;
	System.out.println("useList:  " + duration / 1000000);
	//use set
	startTime = System.nanoTime();
	for (int i = 0; i < 100000; i++) {
		useSet(arr, "A");
	endTime = System.nanoTime();
	duration = endTime - startTime;
	System.out.println("useSet:  " + duration / 1000000);
	//use loop
	startTime = System.nanoTime();
	for (int i = 0; i < 100000; i++) {
		useLoop(arr, "A");
	endTime = System.nanoTime();
	duration = endTime - startTime;
	System.out.println("useLoop:  " + duration / 1000000);


useList:  13
useSet:  72
useLoop:  5

Use a larger array (1k):

String[] arr = new String[1000];
Random s = new Random();
for(int i=0; i< 1000; i++){
	arr[i] = String.valueOf(s.nextInt());


useList:  112
useSet:  2055
useLoop:  99
useArrayBinary:  12

Use a larger array (10k):

String[] arr = new String[10000];
Random s = new Random();
for(int i=0; i< 10000; i++){
	arr[i] = String.valueOf(s.nextInt());


useList:  1590
useSet:  23819
useLoop:  1526
useArrayBinary:  12

Clearly, using a simple loop method is more efficient than using any collection. A lot of developers use the first method, but it is inefficient. Pushing the array to another collection requires spin through all elements to read them in before doing anything with the collection type.

The array must be sorted, if Arrays.binarySearch() method is used. In this case, the array is not sorted, therefore, it should not be used.

Actually, if you need to check if a value is contained in some array/collection efficiently, a sorted list or tree can do it in O(log(n)) or hashset can do it in O(1).

Category >> Array >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. Daniel Drozdzewski on 2014-4-8

    Few things:
    Binary search *requires* the array (or collection) to be sorted in order to perform correctly.

    It is also a false assumption to make about the size of the array. The extrapolation from 5 elements to N elements is wrong. Collections are automatically expanding (after reaching a threshold) which takes extra time when the expansions occur.

    But most of all O(n), O(n^2), and O(log n) are very similar for small values, but diverge by big amounts when n grows “big enough”. Different data structures will allow you to use different algorithms for fetching (or confirming) whether an element is in. Those will come with different costs and caveats.

    Try running your examples for an array of random strings of size 1k, 1M, 10M elements and see the differences then.

  2. Ritter Liu on 2014-4-28

    Wow, what a nice website with so many good articles.

  3. Daniel Gurianov on 2014-5-24

    Typo here :
    4) Using Arrays.binarySearech():

  4. ryanlr on 2014-5-24

    Thank you.

  5. malphix on 2014-9-27

    err:int a = Arrays.binarySearch(arr, targetValue);
    if(a > 0) // a>=0 is correct

  6. Guest on 2014-10-10

    Your demonstration is to show us the performance of finding a value, but you measure more creation time of List, Set. So this article is irrelevant.

  7. Anttix on 2015-1-27

    Profiling with simple loops does not yield meaningful results.

    JVMs are smart and can optimize out code that produces data which is not consumed.
    Also the is the state of JIT caches, the garbage collector runs and other considerations.
    The bottom line is that in order to get meaningful results for small snippets you need to use a more elaborate benchmarking harness. e.g. JMH. See this excellent talk about benchmarking below

  8. I dont know on 2016-3-11

    Is there a way to say “ignore case” while using


  9. wenchao on 2016-3-30

    hashset can do it in O(1)? Why in your case, hastset is the slowest?

  10. Brandy Knapp Smallwood on 2017-2-21

    try this for(x=0;10>x;x++) { any code here}
    it will repeat

Leave a comment