Check if an array elements exist in the another array

Hi,
I have two arrays like this: A={ 1, 3, 7} B={ 4, 5, 2, 3, 8,7,1}
In C++, I want to check whether all elements of the array A exist in the array B or not?
Could you please help me to do this?
Thanks in advance
Try using some for loops to check array B and see if it has any of the values in array A. If you run in to a specific problem post your code and I would be more than happy to help.
Last edited on
can you tell another method without sorting?
Why is sort not possible?



Special case: A={1,1} and B={2,1,3}. Is that a true of false?
because I have some arrays related to each other and sorting one of them can destroy the the relation.
Here is one way to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

bool InArray (int Haystack[], size_t size, int needle);

int main ()
{
  int A[] = { 1, 3, 7 };
  int B[] = { 4, 5, 2, 3, 8, 7, 1 };

  const int kNumElemsInArrayA = sizeof (A) / sizeof (A[0]);
  const int kNumElemsInArrayB = sizeof (B) / sizeof (B[0]);

  for (int i : A)
  {
    if (!InArray (B, kNumElemsInArrayB, i))
    {
      cout << "\aNot all elements are in array B\n\n";
      system ("pause");
      return 0;
    }
  }
  cout << "\All elements are in array B\n\n";
  system ("pause");
  return 0;
}

bool InArray (int Haystack[], size_t size, int needle)
{
  for (size_t i = 0; i < size; i++)
  {
    if (Haystack[i] == needle)
      return true;
  }

  return false;
}
many thanks.
while running in Linux, the following errors are seen.


Error: Invalid type ';return0;}}cout<<"\All' in declaration of 'elements are in array B\n\n"' main.cxx:26:
Error: Symbol ;return0;}}cout<<"\All elements are in array B\n\n" is not defined in current scope main.cxx:26:
Error: improper lvalue main.cxx:26:
*** Interpreter error recovered ***
That is due to:
unknown escape sequence: '\A'

Line 21 does have a '\a'. Line 26 has a '\A'. A typo by Thomas (or a test to see whether you blindly copy-paste sans comprehension).


PS.
Not: "while running in Linux"
but: "when compiling with compiler {brand,version,selected options}"
I am new comer.
I am compiling your code in ROOT (software framework) with Cling (an interactive C++ interpreter) in it.
Even when I comment out the lines 21 and 26 , I encountered with the following error.

Error: Symbol ;return0;}}system is not defined in current scope main.cxx:27:
Error: improper lvalue main.cxx:27:
*** Interpreter error recovered ***

probably, My interpreter has problem with "system ("pause");".
Last edited on
Remove the system("pause"); altogether. There is no sane reason to have it in any program. It is a sorry excuse of a workaround for not knowing how to keep terminal open after a program exists.

See http://www.cplusplus.com/forum/beginner/1988/
and http://www.cplusplus.com/articles/j3wTURfi/

Topic archived. No new replies allowed.