
난이도: ★☆☆☆☆ • solved on: 2026-01-01

자료구조
HashSet : 원소 존재 여부를 평균 O(1)로 검사한다.알고리즘/기법
핵심 키워드
- 문제 분해
- A, B를 각각
HashSet에 넣는다.- A∪B 역할을 하는
setAorB를 만든다.setAorB를 순회하며 A와 B에 모두 있으면(교집합)count++한다.- 최종 답을
|A∪B| - |A∩B|로 계산한다.
핵심 로직 흐름
setAorB = A ∪ B count = |A ∩ B| answer = |setAorB| - count
import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] nums = br.readLine().split(" ");
int n = Integer.parseInt(nums[0]);
int m = Integer.parseInt(nums[1]);
int count = 0;
HashSet<String> setA = new HashSet<>(n);
HashSet<String> setB = new HashSet<>(m);
HashSet<String> setAorB = new HashSet<>();
StringTokenizer stA = new StringTokenizer(br.readLine());
StringTokenizer stB = new StringTokenizer(br.readLine());
while(stA.hasMoreTokens()){
String temp = stA.nextToken();
setA.add(temp);
setAorB.add(temp);
}
while(stB.hasMoreTokens()){
String temp = stB.nextToken();
setB.add(temp);
setAorB.add(temp);
}
for(String a : setAorB){
if(setA.contains(a)&&setB.contains(a)){
count++;
}
}
System.out.println(setAorB.size()-count);
}
}
n + m - 2*common로 바로 계산
- 개선 포인트
- 대칭 차집합 크기 = (|A-B| + |B-A|) 이고, 이는 (|A| + |B| - 2|A∩B|) 로 정리된다.
- 따라서 합집합 set을 따로 만들 필요가 없다.
핵심 로직 흐름
common = 0 A를 set에 저장 B를 읽으면서 A에 있으면 common++ answer = n + m - 2*common추가 개선
- 입력이 자연수이므로
HashSet<Integer>로 처리하는 편이 의미상 맞고, 변환 비용도 줄인다.
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
HashSet<Integer> setA = new HashSet<>(n);
st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
setA.add(Integer.parseInt(st.nextToken()));
}
int common = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < m; i++) {
int x = Integer.parseInt(st.nextToken());
if (setA.contains(x)) common++;
}
int answer = n + m - 2 * common;
System.out.println(answer);
}
}
방법 1
setA, setB, setAorB 저장)방법 2
setA만 유지)비슷한 유형 (GPT 추천):
확장 문제 (GPT 추천):