Find the last non repeating character in a given string.

Objective: Given a string, write an algorithm to find the last non repeating character in it.


String input = "algorithms tutorials"
Output: 'u'

String input = "aabbccdd"
Output: No repeating character found.


Naive approach: This problem can be easily solved using two nested loops from right to left. Take each character from the outer loop and check the character in rest of the string using inner loop and return the first character which is non repeating(from right to left).  Time complexity is O(N^2).

Better approach: Using extra space

  • Iterate the string from right to left.
  • Count the occurrence of each character and store it in a map.
  • Iterate the string again from right to left and check if the character has count = 1 in the map created in the previous step, if yes then return that character.
  • If none of the character has count = 1 in map, return null.

Complete Code:


Last Non Repeating Character in 'algorithms tutorials' is: u

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

  • Santanu Sahoo

    how about use LinkedHashMap and iterate from backwords instead of iterating the whole string from backward. in worst case you might need to iterate the whole string.

  • Johannes Weber

    Another solution in runtime complexity of O(n) and space complexity of O(n) would be using Sets where only one loop is needed.
    Here’s the JavaScript version of it:,console

%d bloggers like this: