I have something I put together real quick, but it searches linearly, so someone could most certainly come up with something more efficient.
Also, I assumed that if both arrays are (for example):
{1, 2, 3, 4, 5, 6}
Then they would just have the two sets {1, 2, 3} and {4, 5, 6}.
Instead of {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}.
Also, there is a high chance that there is a case where the algorithm fails, as I haven't really tested it... so feel free to find a case that doesn't work :P.
PHP Code:
//Returns array containing sets of 3 numbers both arrays have ing common
function ex1(arr1, arr2) {
temp.result = {};
temp.pos = 0;
while (temp.pos < temp.arr1.size() - 2) { //Make sure there's 3 elements left
temp.check = {temp.arr1[temp.pos], temp.arr1[temp.pos + 1], temp.arr1[temp.pos + 2]};
if (temp.result.pos(temp.check) < 0 && searchArr(temp.arr2, temp.check)) {
temp.result.add(temp.check);
temp.pos += 3; //Set is found, jump over whole set
}
else {
temp.pos++; //Set not found, go to next number
}
}
return temp.result;
}
//True if subarr is in arr.
function searchArr(arr, subarr) {
temp.found = 0;
for (temp.e: temp.arr) {
if (temp.e == temp.subarr[temp.found]) {
temp.found++;
if (temp.found == temp.subarr.size()) {
return true;
}
}
else {
temp.found = 0;
}
}
return false;
}