Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,911 questions

51,843 answers

573 users

How to find the uncommon elements from two arrays in C++

1 Answer

0 votes
#include <iostream>

void printUncommonElements(int arr1[], int arr2[], int len1, int len2) {
    int i = 0, j = 0;
    while(i < len1 && j < len2) {
      if(arr1[i] < arr2[j])
         std::cout << arr1[i++] << "\n";
      else if (arr1[i] > arr2[j]) 
         std::cout << arr2[j++] << "\n";
      else { 
         i++;
         j++;
      }
    }
    while(i < len1 && arr1[i] != arr2[j])
      std::cout << arr1[i++] << "\n";
    while(j < len2 && arr2[j] != arr1[i]) 
      std::cout << arr2[j++] << "\n";
}

int main() 
{ 
    int arr1[]= {1, 3, 8, 9, 17, 18};
    int arr2[]= {1, 5, 8, 12, 18, 19, 100, 120};
   
    int len1 = sizeof(arr1) / sizeof(arr1[0]); 
    int len2 = sizeof(arr2) / sizeof(arr2[0]); 
    
    printUncommonElements(arr1, arr2, len1, len2);
} 



/*
run:

3
5
9
12
17
19
100
120

*/

 



answered Jul 19, 2020 by avibootz

Related questions

1 answer 421 views
1 answer 96 views
1 answer 93 views
1 answer 93 views
1 answer 86 views
1 answer 95 views
...