Given a string, remove duplicate characters from the string, retaining the last occurrence of the duplicate characters. Assume the characters are case-sensitive.
Input : geeksforgeeks
Output : forgeks
Explanation : Please note that we keep only last occurrences of repeating characters in same order as they appear in input. If we see result from right side, we can notice that we keep last ‘s’, then last ‘k’ , and so on.
Input : hi this is sample test
Output : hiampl est
Explanation : Here, the output contains last occurrence of every character, even ” “(spaces), and removing the duplicates. Like in this example, there are 4 spaces count, so we have only the last occurrence of space in it removing the others. And there is only last occurrence of each character without repetition.
Input : Abcda
Output : Abcda
Naive Solution : Traverse the given string from left to right. For every character check if it appears on right side also. If it does, then do not include in output, otherwise include it.
Time Complexity: O(n*n)
Efficient Solution : The idea is to use hashing.
1) Initialize an empty hash table and res = “”
2) Traverse input string from right to left. If the current character is not present in the hash table, append it to res and insert it in the hash table. Else ignore it.
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum array from two given arrays keeping order same
- Sort a string according to the order defined by another string
- Group all occurrences of characters according to first appearance
- Find the last remaining Character in the Binary String according to the given conditions
- Sort the array of strings according to alphabetical order defined by another string
- Remove duplicates from a given string
- Remove three consecutive duplicates from string
- Remove all consecutive duplicates from the string
- Remove all duplicates from a given string in Python
- Remove duplicates from a string in O(1) extra space
- Remove all occurrences of a character in a string
- Remove all occurrences of a character in a string | Recursive approach
- Check if words are sorted according to new order of alphabets
- Python groupby method to remove all consecutive duplicates
- Remove duplicates from unsorted array using Map data structure
- Recursively remove all adjacent duplicates
- Remove all occurrences of any element for maximum array sum
- Remove the first and last character of each word in a string
- Remove the first and last occurrence of a given Character from a String
- Remove first and last character of a string in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.