Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด

HashMap ์˜ getOrDefault() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ ๋ฒˆํ˜ธ๊ฐ€ ๋ช‡๋ฒˆ์”ฉ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์ €์žฅํ•œ ํ›„ ์ž…๋ ฅ๋˜๋Š” ์ˆซ์ž๊ฐ€ ํ•ด์‹œ๋งต์— ๋ช‡๊ฐœ ์žˆ๋Š”์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1
2
// 6 3 2 10 10 10 -10 -10 7 3 ์ž…๋ ฅ์ด ์ฃผ์–ด์งˆ๋•Œ
map = {2=1, 3=2, 6=1, 7=1, -10=2, 10=3}

ํ•ด์‹œ๋งต์˜ key ๊ฐ’์œผ๋กœ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๊ณ , value ๊ฐ’์œผ๋กœ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class Main {

  static FastReader scan = new FastReader();
  static StringBuilder sb = new StringBuilder();

  public static void main(String[] args) {
    input();
  }

  static void input() {
    int N = scan.nextInt();
    HashMap<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < N; i++) {
      int key = scan.nextInt();
      map.put(key, map.getOrDefault(key, 0) + 1);
    }
    int M = scan.nextInt();
    for (int i = 0; i < M; i++) {
      sb.append(map.getOrDefault(scan.nextInt(), 0)).append(" ");
    }
    System.out.println(sb);
  }

  static class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    public FastReader(String s) throws FileNotFoundException {
      br = new BufferedReader(new FileReader(new File(s)));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }

}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ