Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. void swap(int *xp, int *yp)  
  2. {  
  3.     int temp = *xp;  
  4.     *xp = *yp;  
  5.     *yp = temp;  
  6. }  
  7.  
  8. void bubbleSort(int arr[], int n)  
  9. {  
  10.     int i, j;  
  11.     for (i = 0; i < n-1; i++)      
  12.      
  13.     // Last i elements are already in place  
  14.     for (j = 0; j < n-i-1; j++)  
  15.         if (arr[j] > arr[j+1])  
  16.             swap(&arr[j], &arr[j+1]);  
  17. }  
  18.  
  19. int singleNumber(int* nums, int numsSize){
  20.     int i = 0, found = 0, o = 0, aux;
  21.    
  22.     bubbleSort(nums, numsSize);
  23.    
  24.     if (nums[0] != nums[1]) {
  25.         found = nums[0];
  26.     }
  27.    
  28.     if (nums[numsSize - 1] != nums[numsSize - 2]) {
  29.         found = nums[numsSize - 2];
  30.     }
  31.    
  32.     for (i = 1; i < numsSize - 2; i++) {
  33.         if (nums[i] != nums[i - 1]) {
  34.             if (nums[i] != nums[i + 1]) {
  35.                 found = nums[i];
  36.             }
  37.         }
  38.     }
  39.    
  40.     return found;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement