
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print uncommon elements from two sorted arrays
Given two sorted arrays and output should display their uncommon elements
Given : array1[]= {1, 4, 6, 9, 12} array2[]= {2, 4, 7, 8, 9, 10} Output : 1 2 6 7 8 10 12
Algorithm
START Step 1 -> declare two arrays array1 and array2 with elements as int and variables n1, n2, i to 0 and j to 0 Step 2 -> calculate number of elements in array1 sizeof(array1)/sizeof(array1[0]) Step 3-> calculate number of elements in array2 sizeof(array2)/sizeof(array2[0]) Step 4 -> Loop While till i<n1 and j<n2 IF array1[i]<array2[j] Print array1[i++] End IF ELSE If array1[i] > array2[j] Print array2[j++ ] End ELSE IF ELSE i++ and j++ End ELSE Step 5 -> End Loop While Step 6 -> loop While i < n1 && array1[i]!=array2[j] Print array1[i++] Step 7 -> End Loop While Step 8 -> loop While j < n2 && array2[j]!=array1[i] Print array2[j++] Step 9 -> End Loop While STOP
Example
#include <stdio.h> int main(int argc, char const *argv[]) { int array1[]= {1, 4, 6, 9, 12}; int array2[]= {2, 4, 7, 8, 9, 10}; int n1, n2, i=0, j=0; n1 = sizeof(array1)/sizeof(array1[0]); //Calculating number of elements in array1 n2 = sizeof(array2)/sizeof(array2[0]); //Calculating number of elements in array2 while(i < n1 && j < n2) { if(array1[i] <array2[j]) //checking whether the element of array1 is smaller than array2 printf("%d
", array1[i++]); else if (array1[i] > array2[j]) //checking whether the element of array2 is smaller than array1 printf("%d
", array2[j++]); else { //if they are equal increment both i and j i++; j++; } } while(i < n1 && array1[i]!=array2[j]) //print remaining array1 printf("%d
", array1[i++]); while(j < n2 && array2[j]!=array1[i]) //print remaining array1 printf("%d
", array2[j++]); return 0; }
Output
If we run the above program then it will generate the following output
1 2 6 7 8 10 12
- Related Articles
- Golang Program to find the uncommon elements from two arrays
- Program to find uncommon elements in two arrays - JavaScript
- JavaScript Program for find common elements in two sorted arrays
- Find three closest elements from given three sorted arrays in C++
- Find the closest pair from two sorted arrays in c++
- Merge two sorted arrays in Java
- Merge two sorted arrays using C++.
- Merge two sorted arrays in C#
- Python Program for Find the closest pair from two sorted arrays
- Java Program to Find the closest pair from two sorted arrays
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Merging two sorted arrays into one sorted array using JavaScript
- Median of Two Sorted Arrays in C++
- Find common elements in three sorted arrays in C++
- Merge two sorted arrays in Python using heapq?

Advertisements