bactoria 2018. 1. 9. 19:38

알고리즘문제중

 Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.


이때 set을 이용하여

  for (int i = 0; i < n; i++) {
                temp = sc.nextInt();
                if(set.contains(temp)) {
                    set.remove(temp);
                }else {
                    set.add(temp);
                }
            }


contains 를 이용하여 체크를 할수 있겠지만


add() 메소드 자체적으로 boolean값을 반환한다.

고로..

 if ( !set.add(num) ) set.remove(num);


이렇게 나타낼 수 있다.