I am getting TLE . plz tell what is wrong in my code?

import java.util.*;

public class Main {

public static void main(String[] args) {

	Scanner scn=new Scanner(System.in);

    int n=scn.nextInt();
    int [] arr=new int[n];
    int max=0;
    for (int i=0;i<n;i++) {
    	arr[i]=scn.nextInt();
    	if(arr[i]>max) {
    		max=arr[i];
    	}
    }
    
    int[] ans=hash_array(arr, arr.length,max);
    HashMap<Integer,Integer> map=new HashMap<>();
    for(int j=0;j<ans.length;j++) {
    	if(ans[j] !=-1) {
    		map.put(arr[j], j);
    	}
    }
    ArrayList<Integer> keys=new ArrayList<>(map.keySet());
    Collections.sort(keys);
    for(int key:keys) {
    	System.out.println(map.get(key));
    }
    
}

public static int[] hash_array(int A[], int n,int MAX) {
	int[] hash = new int[MAX+1]; 
	for (int i = 0; i <= MAX; i++)
		hash[i] = -1; 
	int count = 0;
	for (int i = 0; i < n; i++) 
	{
		if (hash[A[i]] == -1) 
		{
			hash[A[i]] = count; 
			count++;
			continue;
		}
		for (int j = 0; j < i; j++) {
			if (hash[A[j]] > hash[A[i]])
				hash[A[j]]--;
		}
		hash[A[i]] = count - 1; 
	}
	return hash;
}

}

Their is a better approach nLog(n).
We know everytime a number repeats, it is given a new hash value, which is equal to the number of distinct numbers occurred till now. So we can just store the last occurrences of all the numbers and assign the values incrementally, in increasing order of the positions of their last occurrences.

refer to this code : https://ide.codingblocks.com/s/291013
you can watch this video too
https://www.youtube.com/watch?v=98eH6uAIeMs