
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find array after removal of left occurrences of duplicate elements
Suppose we have an array A with n elements. We want to remove duplicate elements. We want to leave only the rightmost entry for each element of the array. The relative order of the remaining unique elements should not be changed.
So, if the input is like A = [1, 5, 5, 1, 6, 1], then the output will be [5, 6, 1]
Steps
To solve this, we will follow these steps −
Define two arrays b and vis of size: 1200 each x := 0 n := size of A for initialize i := n - 1, when i >= 0, update (decrease i by 1), do: if vis[A[i]] is zero, then: b[x] := A[i] (increase x by 1) vis[A[i]] := 1 for initialize i := x - 1, when i >= 0, update (decrease i by 1), do: print b[i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A) { int b[1200], vis[1200], x = 0; int n = A.size(); for (int i = n - 1; i >= 0; i--) { if (!vis[A[i]]) { b[x] = A[i]; x++; vis[A[i]] = 1; } } for (int i = x - 1; i >= 0; i--) cout << b[i] << ", "; } int main() { vector<int> A = { 1, 5, 5, 1, 6, 1 }; solve(A); }
Input
{ 1, 5, 5, 1, 6, 1 }
Output
5, 6, 1,
- Related Articles
- C++ Program to find array after removal from maximum
- C++ program to find reduced size of the array after removal operations
- Program to find winner of array removal game in Python
- Python program to print the duplicate elements of an array
- Program to find mean of array after removing some elements in Python
- C# program to find all duplicate elements in an integer array
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Python program to left rotate the elements of an array
- Write a Golang program to find duplicate elements in a given array
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- Program to find duplicate item from a list of elements in Python

Advertisements