취미처럼

[Codility] Lesson 2. Arrays - OddOccurrencesInArray 본문

Algorithm/Codility

[Codility] Lesson 2. Arrays - OddOccurrencesInArray

sirius 2020. 2. 19. 21:48

배열 중 같은 수로써 짝이 맞지 않는 요소를 구하는 문제

 

public int solution(int[] A) {
  int answer = 0;
  for (int i = 0; i < A.length; i++) {
 	 answer ^= A[i];
  }
  return answer;
}
	

 

XOR 연산은 같은 수일 때 0을 리턴한다.

Comments