Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 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 || duplactedNumber.positionOfDuplicate == NULL) ; numberChecking++)
  11.     {
  12.         for(int potentialDuplicate = numberChecking + 1 ; potentialDuplicate<duplactedNumber.positionOfDuplicate || potentialDuplicate < a.size();potentialDuplicate++)
  13.         {
  14.             if(a.at(numberChecking) == a.at(potentialDuplicate) && (numberChecking < duplactedNumber.positionOfDuplicate || duplactedNumber.positionOfDuplicate == NULL))
  15.             {
  16.                 duplactedNumber.positionOfDuplicate = potentialDuplicate;
  17.                 duplactedNumber.duplicateValue = a.at(potentialDuplicate);
  18.                 break;
  19.             }
  20.         }
  21.     }
  22.    
  23.     if(duplactedNumber.duplicateValue == NULL)
  24.     {
  25.         return -1;
  26.     }
  27.     else
  28.     {
  29.         return duplactedNumber.duplicateValue;        
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement