[ 다먹살 ]/- Coding

[백준] 1269 대칭 차집합

엉망으로살기 2022. 1. 10. 11:26
반응형

https://www.acmicpc.net/problem/1269

 

1269번: 대칭 차집합

첫째 줄에 집합 A의 원소의 개수와 집합 B의 원소의 개수가 빈 칸을 사이에 두고 주어진다. 둘째 줄에는 집합 A의 모든 원소가, 셋째 줄에는 집합 B의 모든 원소가 빈 칸을 사이에 두고 각각 주어

www.acmicpc.net

 

차집합이라는 것은 어쨌든 집합 간의 차를 생각하면 되는 것이기 때문에 수의 중복여부만 체크해주면 되는 문제였다. 나는 HashSet을 이용해 입력 받은 값을 키로 생각해서 해결했다.


문제 및 입출력


코드

import java.util.Scanner;
import java.util.HashSet;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        HashSet<Integer> set = new HashSet<Integer>();
        
        for(int i=0; i<n+m; i++)
        {
            int input = sc.nextInt();
            
            if(set.contains(input))
            {
                set.remove(input);
            }
            else
            {
                set.add(input);
            }
        }
        
        System.out.println(set.size());
        sc.close();
    }
}

 

반응형