Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. int firstDuplicate(std::vector<int> a) {
  2.     struct duplicate
  3.     {
  4.         int positionOfDuplicate = NULL;
  5.         int duplicateValue = NULL;
  6.     };
  7.    
  8.     duplicate duplactedNumber;
  9.    
  10.     for(int numberChecking = 0 ; numberChecking<a.size() && (numberChecking < duplactedNumber.positionOfDuplicate ||         
  11.     duplactedNumber.positionOfDuplicate == NULL) ; numberChecking++)
  12.     {
  13.         for(int potentialDuplicate = numberChecking + 1 ; (potentialDuplicate<duplactedNumber.positionOfDuplicate ||
  14.         duplactedNumber.positionOfDuplicate == NULL)
  15.         && potentialDuplicate < a.size() ; potentialDuplicate++)
  16.         {
  17.             if(a.at(numberChecking) == a.at(potentialDuplicate)
  18.             && (numberChecking < duplactedNumber.positionOfDuplicate || duplactedNumber.positionOfDuplicate == NULL))
  19.             {
  20.                 duplactedNumber.positionOfDuplicate = potentialDuplicate;
  21.                 duplactedNumber.duplicateValue = a.at(potentialDuplicate);
  22.                 break;
  23.             }
  24.         }
  25.     }
  26.    
  27.     if(duplactedNumber.duplicateValue == NULL)
  28.     {
  29.         return -1;
  30.     }
  31.     else
  32.     {
  33.         return duplactedNumber.duplicateValue;        
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement