简体   繁体   中英

What is the best way to check if ALL values in a range exist in an array in C?

How do I test that an array contains at least once, every integer value from 1 to 3? for example:

#include <stdio.h>

int main()
{
  int array[7] = {2, 3, 4, 6, 5, 1};
  if (/* array contains 1, 2 and 3 */) {
    printf("TRUE\n");
  } else {
    printf("FALSE\n");
  }
}

Thanks for your help in advance!

You first need to decide how to specify the range you want. For instance, do you want to enumerate each value or do you simply want to specify the minimum and maximum values?

For instance, if you only want to use contiguous values then specifying the minimum and maximum values will be satisfactory. On the other hand, if you want to specify the first eight even numbers, or the first ten prime numbers, then a minimum and maximum value are not as much help, and an enumerated list may be more useful.

Consider defining a struct containing a single value in the value series you want to identify along with a count of the occurrences of that value. Construct an array of those structs sufficiently large to represent each value in your value series.

Now iterate through the array you want to study. Compare each array value with values in your array of structs. If it matches a value increment the corresponding count.

After examining each value in the array you want to study examine the array of structs. If all the counts in the array of struct are greater than 0 your original array contains at least one instance of every value in the series. If one or more of the counts is 0 then one or more of the values is missing from the array you want to study.

what do you think of this? Basically, you create an array with the values you want to find in your array. You create a function to do that and you send your array and the values you want to find in it. Also send the size of each array to avoid errors. Then for each value you try to find, iterate on the array to find it. If you go threw the whole array without finding it, return false cause your value is not inside your array. If you never return false in your for loop, it means you always found the value, therefore you can return true. All your values are inside the array.

bool   areValuesInArray(int *array, int *value, int sizeArray, int sizeValue)
{
    int j;
    for (int i = 0, i < sizeValue; i++)
    {
        j = 0;
        while (j < sizeArray && array[j] != values[i])
             j++;
        if (j == sizeArray)
             return false;
    }
    return true;
}

there are 2 ways, either use the map function to check for all the 3 variables, but the issue is this might take longer with lot of data

so probably you can use the includes method.

so it becomes array.includes(1) .This returns a boolean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM