
- 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 the last occurrence of elements in array in relative order in C Program.
Given an array a[] with elements and the task is to print the last occurrences of the given elements in the list. Here we not only have to remove the duplicate elements but also we have to maintain the order of the occurrences of the elements in an array as per the last time they have occurred.
Like we have an array of 6 elements also containing some duplicate values i.e., {1,3, 2, 3, 1, 2} so the result should be in form of 3 1 2.
Example
Input: a[]={4,2,2,4,1,5,1} Output : 2 4 5 1
Algorithm
START Step 1-> Declare function void printelements(int a[], int n) Use STL unordered_map<int, int> ele Loop For int i=0 and i<n and i++ Set ele[a[i]]=i Loop For int i=0 and i<n and i++ IF ele[a[i]]=i Print a[i] End End Step 2 -> main() Declare array a[]={4,2,2,4,1,5,1} Declare int n=sizeof(a)/sizeof(a[0]) Call Function printelements(a,n) STOP
Example
#include <bits/stdc++.h> using namespace std; void printelements(int a[], int n) { unordered_map<int, int> ele; for (int i = 0; i < n; i++) ele[a[i]] = i; for (int i = 0; i < n; i++) { if (ele[a[i]] == i) cout << a[i] << " "; } } int main() { int a[] = { 4,2,2,4,1,5,1 }; int n = sizeof(a) / sizeof(a[0]); printelements(a, n); return 0; }
Output
if we run above program then it will generate following output
2 4 5 1
- Related Articles
- Python program to print the elements of an array in reverse order
- Print characters having odd frequencies in order of occurrence in C++
- Print characters and their frequencies in order of occurrence in C++
- C# Program to order array elements in descending order
- Print array elements in alternatively increasing and decreasing order in C++
- Finding relative order of elements in list in Python
- Program to find duplicate elements and delete last occurrence of them in Python
- How to print the elements in a reverse order from an array in C?
- C++ Program to Sort the Elements of an Array in Descending Order
- C++ Program to Sort the Elements of an Array in Ascending Order
- C# Program to order array elements
- Program to print matrix elements in spiral order in python
- C# Program to display the last three elements from a list in reverse order
- Print n smallest elements from given array in their original order
- Program to print last 10 lines in C++

Advertisements