import java.util.*;
class Main {
public int[] solution(int S, int N, int[] arr) {
int[] cache = new int[S];
for (int i = 0; i < N; i++) {
boolean check = true;
int tmp = arr[i];
for (int j = 0; j < S; j++) {
if (tmp == cache[j]) {
check = false;
for (int k = j; k > 0; k--) {
cache[k] = cache[k - 1];
}
cache[0] = tmp;
}
}
if (check) {
for (int j = S - 1; j > 0; j--) {
cache[j] = cache[j - 1];
}
cache[0] = tmp;
}
}
return cache;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int S = kb.nextInt();
int N = kb.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++)
arr[i] = kb.nextInt();
for (int x : T.solution(S, N, arr))
System.out.print(x + " ");
kb.close();
}
}
import java.util.*;
class Main {
public int[] solution(int S, int N, int[] arr) {
int[] cache = new int[S];
for (int x : arr) {
int pos = -1;
for (int i = 0; i < S; i++) {
if (x == cache[i])
pos = i;
}
if (pos == -1) {
for (int i = S - 1; i > 0; i--) {
cache[i] = cache[i - 1];
}
} else {
for (int i = pos; i > 0; i--) {
cache[i] = cache[i - 1];
}
}
cache[0] = x;
}
return cache;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int S = kb.nextInt();
int N = kb.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++)
arr[i] = kb.nextInt();
for (int x : T.solution(S, N, arr))
System.out.print(x + " ");
kb.close();
}
}
댓글