fork download
  1. #include <stdio.h>
  2.  
  3. #define ARR1SIZE 5
  4. #define ARR2SIZE 5
  5.  
  6. int main()
  7. {
  8. int arr1[] = {6, 3, 6, 1, 7};
  9. int arr2[] = {5, 1, 2, 4, 6};
  10. int arr3[ARR1SIZE + ARR2SIZE];
  11.  
  12. // Using bit map, All bits are 0
  13. int bitMap = 0;
  14. int bitMask = 0;
  15. int i = 0;
  16. int k = 0;
  17.  
  18. for (i = 0; i < ARR1SIZE; i++) {
  19. // Check if value already exist
  20. bitMask = 0;
  21. bitMask = 1 << arr1[i];
  22.  
  23. if (bitMap & bitMask) {
  24. continue;
  25. }
  26. arr3[k++] = arr1[i];
  27.  
  28. // Mark bitMap that value exist
  29. bitMap |= 1 << arr1[i];
  30. }
  31.  
  32. for (i = 0; i < ARR2SIZE; i++) {
  33. // Check if value already exist
  34. bitMask = 0;
  35. bitMask = 1 << arr2[i];
  36.  
  37. if (bitMap & bitMask) {
  38. continue;
  39. }
  40. arr3[k++] = arr2[i];
  41.  
  42. // Mark bitMap that value exist
  43. bitMap |= 1 << arr2[i];
  44. }
  45.  
  46. printf("Merged Array\n");
  47. for (i = 0; i < k; i++) {
  48. printf("%d ", arr3[i]);
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
Merged Array
6 3 1 7 5 2 4