How to check if one array exists exactly same in another array in C

Viewed 46

Example 1.

input :  
array1[8] = { 3, 12, 1, 12, 7, 8, 19, 13}
array2[4] = {12, 7, 8, 19}
output : 1

Example 2.

input: 
array1[8] = {3, 12, 1, 12, 7, 8, 19, 13}
array2[3] = {12, 7, 19}
output: 0

In the first example, array2 is contained in order, but in the second example there is an 8 between 7 and 19, so array2 it is not found in order.

1 Answers

loop over the first array (a1). Compare each element with the first element of second array (a2)

If they match compare the next element of a1 with next element of a2, keep going till end of either array or no match. If no match then go to next elemnent in a1. Repeat till end of a1.

Related