Uncommon elements of two arrays

Code to make another array of the array differences like -
array1[] = 1, 2, 3, 4, 5, 6, 7, 8, 9
(Note array 1 value is always this only)
array2[] = 3, 4, 5, 6, 1
(array 2 have less or equal numbers than arrays 1 and has numbers 1, 2, 3, 4, 5, 6, 7, 8, or 9. None of the number repeats)
So I want array 3 to be -
array3[] = 2, 7, 8, 9

Plz help for this code

Have you considered a nested for loop. Might not be the most efficient but will work.

Are the arrays sorted? (Looks like they are not)

In which case for each entry in the second array you need to look each element of the first and if you can't find it then add it to a third array. Two nested loops indeed

If arrays are sorted then you can stop earlier and don't need to scan the whole array and there are smart approaches

this code would do what you want in the general case where array1 in not just a sequence of numbers from 1 to 9.

int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int array2[] = {3, 4, 5, 6, 1};
int array3[sizeof(array1) / sizeof(array1[0])];


void setup() {
  Serial.begin(115200);
  byte i, j, k = 0;
  byte size1 = sizeof(array1) / sizeof(array1[0]);
  byte size2 = sizeof(array2) / sizeof(array2[0]);

  for (i = 0; i < size1 ; i++) {
    for (j = 0; j < size2; j++)
      if (array1[i] == array2[j]) break;
    if (j == size2) array3[k++] = array1[i];
  }
  Serial.print(k); Serial.println(" Non shared elements:");
  for (byte i = 0; i < k; i++) Serial.println(array3[i]);
}

void loop() {}

in your case since you know array1 does not change and is always 1 to 9 then array1 is useless and the code could look like this:

int array2[] = {3, 4, 5, 6, 1};
int array3[9];


void setup() {
  Serial.begin(115200);
  byte i, j, k = 0;
  byte size2 = sizeof(array2) / sizeof(array2[0]);

  for (i = 1; i < 10 ; i++) {
    for (j = 0; j < size2; j++)
      if (i == array2[j]) break;
    if (j == size2) array3[k++] = i;
  }
  Serial.print(k); Serial.println(" Non shared elements:");
  for (byte i = 0; i < k; i++) Serial.println(array3[i]);
}

void loop() {}

J-M-L you are the best. Your both codes worked perfect and was pretty simple code. A thousand thx for the code. It took me a day to make the same but it was very complex but you are the best. Thank you very, very much.

If none of the numbers are repeated in either array, drop both arrays into a single array with memcpy, sort it with an of-the-shelf sort routine, and scan it for duplicates.

Or, if the values are known to never be greater than a given value, scan each array and make up a bit-map of the values in it, then bitwise and then to get the duplicates.

Many ways to skin this particular cat.

Good point

The bit-map approach in that instance would be of interest as OP said only values between 1 and 9 are legit.

So take an uint16_t bitmap; initialized to 0. scan each arrray2 entry and set the bit nb according to the array2 value, then build up array3 by looking at the bits set to 0 for bits 1 to 9. Will be faster than the two nested loops

Because of you all I made a perfect tic-tac-toe analysing engine. Just I was slow and took 6 seconds to analyse the whole game but I used 7 atmega chips and it was all good. I next prepared it by memory and used 5 chips of eeprom and was very fast. I am still improving the code. I am a hardware student but bad in coding.

Thunderer:
Because of you all I made a perfect tic-tac-toe analysing engine.

Oh, is that what's going on!

Dude - model your board as a pair of 16-bit integers named "exes" and "ohs", and use the bits 0-8 for positions on the board. Learn to use the C++ bit-fiddling operators: ~ & | ^ << >> >>>. If you are a hardware dude, there's not a lot of explanation needed.

For instance, say someone touches cell number celltouch. This expression:

 (exes | ohs) & (1 << celltouch)

will be zero if cell celltouch is not already filled, and nonzero if it is. So you can use it as an if() condition.