문제 출처 : https://www.acmicpc.net/problem/18870
위의 문제를 접근할 때 hashmap을 활용해서 문제를 해결했습니다.
각 배열의 수를 hashmap의 키(중복되는 수는 한번만)로 설정했고 그에 대한 값은 0부터 시작해 1씩 증가하며 hashmap 추가했습니다.
코드를 입력하세요import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int cnt = 0;
Map<Integer, Integer> arr1 = new HashMap<>();
ArrayList<Integer> arr2 = new ArrayList<>();
int[] check = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++){
int num = Integer.parseInt(st.nextToken());
arr2.add(num);
check[i] = num;
}
Collections.sort(arr2);
for(int i = 0; i < arr2.size(); i++){
if(arr1.keySet().contains(arr2.get(i))){
continue;
}
else{
arr1.put(arr2.get(i),cnt++);
}
}
StringBuilder sb = new StringBuilder();
for(int key : check) {
sb.append(arr1.get(key)).append(" ");
}
System.out.println(sb);
}
}