Saturday, 16 Oct 2021

# Print all unique elements present in a sorted array

Given a sorted array arr[] of size N, the task is to print all the unique elements in the array.An array element is said to be unique if the frequency of that element in the array is 1.Examples:Input: arr[ ] = {1, 1, 2, 2, 3, 4, 5, 5}Output: 3 4Explanation: Since 1, 2, 5 are occurring more than once in the array, the distinct elements are 3 and 4.Input: arr[ ] = {1, 2, 3, 3, 3, 4, 5, 6}Output: 1 2 4 5 6Approach: The simplest approach to solve the problem is to traverse the array arr[] and print only those elements whose frequency is 1. Follow the steps below to solve the problem:Iterate over the array arr[] and initialize a variable, say cnt = 0,   to count the frequency of the current array element.Since the array is already sorted, check if the current element is the same as the previous element. If found to be true, then update cnt += 1.Otherwise, if cnt = 1, then print the element. Otherwise, continue.Below is the implementation of the above approach:C++#include using namespace std;  void RemoveDuplicates(int arr[], int n){      int i = 0;          while (i < n) {          int cur = arr[i];                          int cnt = 0;                                          while (i < n and cur == arr[i]) {            cnt++;            i++;        }                  if (cnt == 1) {              cout