근본적으로 문제를 생각했을 때 둘의 교집합을 구하고 [A집합 - 교집합] + [B집합 - 교집합] 을 구하면 된다는 결론이 나왔다.
즉, A집합 + B집합 - 교집합 X 2 를 해주면 된다.
교집합을 구하는 방식은 HashMap의 containsKey를 사용했고 풀이는 아래와 같다.
소스코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.HashMap;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
HashMap<Integer, Integer> map = new HashMap<>();
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int count = 0;
int S = N+M;
st = new StringTokenizer(br.readLine(), " ");
while(N-- >0)
{
map.put(Integer.parseInt(st.nextToken()), 0);
}
st = new StringTokenizer(br.readLine(), " ");
while(M-- >0)
{
if(map.containsKey(Integer.parseInt(st.nextToken()))) count++;
}
System.out.println(S-2*count);
}
}