Find single int among vector of duplicates in c++ -


this question has answer here:

i writing function in c++ evaluate vector n integers. every integer has 1 duplicate value somewhere in vector exception of 1 single integer has no duplicates. function should return 1 int no duplicate values in array.

i managed make working function think runs in either o(nlogn) time or in o(n^2), i'm not sure which. need function run in o(n) time, how can change function achieve such run time?

int singlenumber(vector<int>& nums) {     int s = nums.size();      (int = 0; < s; i++)     {         int current = i;  // current int in vector being evaluated         bool f1 = false, f2 = false;          (int j = 0; j < s; j++)         {             if (current == nums[j] && !f1)  // if 1 instance of current found                 f1 = true;             else if (f1 && current == nums[j]) // if 2nd instance of current found                 f2 = true;         }          if (f1 && !f2)    // if 1 instance of current found             return current;     }   } 

i think you'll end number if xor on elements.


Comments