CodeWars 네 번째 문제

Updated:

Find the odd int

public class FindOdd {
  public static int findIt(int[] a) {
    int odd = 0;
    Arrays.sort(a);
    
    for(int i = 0; i < a.length; ) {
      odd = a[i];
      int time = 0;
      
      for(int j = i ; a[j] == odd; i = j) {
        time++;
        
        if(++j == a.length) {
          break;
        }
      }
      
      if(time % 2 != 0) {
        break;
      }     
    }
    
      return odd;
  }
}

*XOR

이 문제는 XOR로 간단하게 풀린다. 아니 XOR로 풀리는건 알겠는데 이 문제 조건으로

XOR이 생각할 수 있다는게 나는 이해가 안감….

문제조건 : 배열의 숫자 중에서 홀수개의 숫자가 한 개만 존재

public class FindOdd {
	public static int findIt(int[] a) {
		
		int odd = 0;
		
		for(int i : a) {
			
			odd ^=i;
		}
		
	  	return odd;
	}
}