class Solution {
public int singleNumber ( int[ ] nums) {
int num = 0 ;
if ( nums. length == 1 ) return nums[ 0 ] ;
QuickSort ( nums, 0 , nums. length - 1 ) ;
if ( nums[ 0 ] != nums[ 1 ] ) return nums[ 0 ] ;
if ( nums[ nums. length - 1 ] != nums[ nums. length - 2 ] ) return nums[ nums. length - 1 ] ;
for ( int i = 2 ; i < nums. length - 1 ; i++ ) {
if ( nums[ i] != nums[ i - 1 ] && nums[ i] != nums[ i + 1 ] ) num = nums[ i] ;
else if ( nums[ i] == nums[ i + 1 ] && nums[ i] != nums[ i - 1 ] ) i++ ;
else num = nums[ ++ i] ;
}
return num;
}
public static void QuickSort ( int[ ] arr, int start, int end) {
if ( start < end) {
int low = start;
int high = end;
int standard = arr[ low] ;
while ( low < high) {
while ( low < high && standard < arr[ high] ) {
high-- ;
}
if ( low < high) arr[ low++ ] = arr[ high] ;
while ( low < high && standard > arr[ low] ) {
low++ ;
}
if ( low < high) arr[ high-- ] = arr[ low] ;
}
arr[ low] = standard;
QuickSort ( arr, start, low - 1 ) ;
QuickSort ( arr, low + 1 , end) ;
}
}
}
var singleNumber = function ( nums) {
nums. sort ( ( x, y) => x - y) ;
for ( let i = 0 ; i < nums. length; i++ ) {
if ( ( i === nums. length - 1 || nums[ i] !== nums[ i + 1 ] ) && ( i === 0 || nums[ i] !== nums[ i - 1 ] ) ) {
return nums[ i] ;
}
}
} ;
public int singleNumber ( int[ ] nums) {
int ans = 0 ;
for ( int n: nums) ans ^= n;
return ans;
}