# 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.

Example:

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

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

Approach:

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:

 import java.util.HashMap; public class LastNonRepeatingCharacter { public static Character getCharacter(String input){ //remove all the spaces input = input.replaceAll(" ", ""); Character nonRptChar = null; //Will store each character and it's count HashMap map = new HashMap<>(); for (int i = 0; i =0; i—) { if(map.get(input.charAt(i))==1){ nonRptChar = input.charAt(i); break; } } return nonRptChar; } public static void main(String[] args) { String input = "algorithms tutorial"; Character result = getCharacter(input); if(result!=null){ System.out.println("Last Non Repeating Character in '"+input+"' is: " + result); }else{ System.out.println("No Repeating Character found"); } } }

Output:

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