Check if a value exists in an array

Is there a predefined function for this? Thanks in advance.

Easy enough to write code to do that

for (x=0; x<arraySize; x=x+1){
if (arrayName[x] == compareValue){
  // do something
 // x = arraySize; // force end of array check
  }
}

'break' is probably a bit cleaner than mucking with the index to "force end of array check"

@jay98 Can the value exist multiple times in the array ?

Can the contents of the array be said to have any simply defined order? If so, a binary search may allow a quicker search.

Thanks for all the answers, in the case I'm working right now it's an integers array, and yes, the same value can exist more than once. I know this can be coded easy, but it's always better a predefined function if there is one, that's why I asked.

AWOL: Can the contents of the array be said to have any simply defined order?

I'm afraid I don't understand what that means. :confused:

In other words, are they sorted?

Jay98: I know this can be coded easy, but it's always better a predefined function if there is one, that's why I asked.

The Standard Template Library (STL) has such a function but the STL is not installed by default.

KeithRB: In other words, are they sorted?

No, they aren't.

[quote author=Nick Gammon link=msg=2237762 date=1431984908] The Standard Template Library (STL) has such a function but the STL is not installed by default. [/quote]

Thank you Nick.

Questions to ask are:

Is the array sorted?
Can the value occur more than once?
If it occurs more than once, what do you want to do? Do you want to do a thing for each item, or do you just want a yes/no?

A sorted array is very easy and very efficient to search using a binary chop search. If “is this item in my array?” is something that is going to be heavily spammed, and the array is not sorted, and it’s not too volatile, it can be worthwhile keeping a second array of sorted indexes into the first.