# \[99클럽 코테 스터디 14일차 TIL]  백준 - 숫자 카드 2

## \[Silver IV] 숫자 카드 2 - 10816

[문제 링크](https://www.acmicpc.net/problem/10816)

#### 분류

이분 탐색, 자료 구조, 해시를 사용한 집합과 맵, 정렬

#### 문제 설명

숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 몇 개 가지고 있는지 구하는 프로그램을 작성하시오.

#### 입력

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다.

셋째 줄에는 M(1 ≤ M ≤ 500,000)이 주어진다. 넷째 줄에는 상근이가 몇 개 가지고 있는 숫자 카드인지 구해야 할 M개의 정수가 주어지며, 이 수는 공백으로 구분되어져 있다. 이 수도 -10,000,000보다 크거나 같고, 10,000,000보다 작거나 같다.

#### 출력

첫째 줄에 입력으로 주어진 M개의 수에 대해서, 각 수가 적힌 숫자 카드를 상근이가 몇 개 가지고 있는지를 공백으로 구분해 출력한다.

***

## 풀이 코드

```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Map<String, Integer> map = new HashMap<>();
        String n = br.readLine();
        String[] cards = br.readLine().split(" ");
        for (String card : cards) {
            map.put(card, map.getOrDefault(card, 0) + 1);
        }
        String m = br.readLine();
        String[] numbers = br.readLine().split(" ");
        StringBuilder sb = new StringBuilder();
        for (String number : numbers) {
            if (map.containsKey(number)) sb.append(map.get(number)).append(" ");
            else sb.append(0).append(" ");
        }

        System.out.println(sb);
    }
}
```

* Time: 816 ms
* Memory: 211924 KB

앞선 13일차의 숫자 카드 문제에서 가지고 있는지만 구분하는 것이 아니라 몇개 가지고 있는지 알아내는 문제로 변형된 문제이다. 풀이 접근 방식은 비슷하다. 이번엔 `HashMap` 을 활용하여 풀어봤다.

배열을 활용한 풀이도 작성해봤다. 속도면에선 확실히 더 빠르다.

```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int[] arr = new int[20_000_001];
        int n = Integer.parseInt(br.readLine());
        String[] cards = br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            arr[Integer.parseInt(cards[i]) + 10_000_000]++;
        }
        int m = Integer.parseInt(br.readLine());
        String[] numbers = br.readLine().split(" ");
        for (int i = 0; i < m; i++) {
            sb.append(arr[Integer.parseInt(numbers[i]) + 10_000_000]).append(" ");
        }
        System.out.println(sb);
    }
}
```

이전 숫자 카드 풀이에서는 `boolean` 배열을 생성해서 있으면 `true` 로 변환했다면 이번 풀이에서는 `int` 배열로 선언해서 해당 인덱스의 값을 1씩 증분한다.

***

\#99클럽 #코딩테스트준비 #개발자취업 #항해99 #TIL


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devlee.gitbook.io/docs/study/99/99-14-til-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
