|
| 1 | +//problem :-https://www.hackerrank.com/challenges/lonely-integer/problem |
| 2 | +//Platfrom :- Hackerrank |
| 3 | + |
| 4 | +#include <bits/stdc++.h> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +vector<string> split_string(string); |
| 9 | +//Here is the main code |
| 10 | +int lonelyinteger(vector<int> a) { |
| 11 | + int ans=0; |
| 12 | + //taking xor of all numbers |
| 13 | + //xor of same number will become Zero |
| 14 | + for(int i=0;i<a.size();i++) |
| 15 | + { |
| 16 | + ans^=a[i]; |
| 17 | + } |
| 18 | +return ans; |
| 19 | +} |
| 20 | + |
| 21 | +int main() |
| 22 | +{ |
| 23 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 24 | + |
| 25 | + int n; |
| 26 | + cin >> n; |
| 27 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 28 | + |
| 29 | + string a_temp_temp; |
| 30 | + getline(cin, a_temp_temp); |
| 31 | + |
| 32 | + vector<string> a_temp = split_string(a_temp_temp); |
| 33 | + |
| 34 | + vector<int> a(n); |
| 35 | + |
| 36 | + for (int i = 0; i < n; i++) { |
| 37 | + int a_item = stoi(a_temp[i]); |
| 38 | + |
| 39 | + a[i] = a_item; |
| 40 | + } |
| 41 | + |
| 42 | + int result = lonelyinteger(a); |
| 43 | + |
| 44 | + fout << result << "\n"; |
| 45 | + |
| 46 | + fout.close(); |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +vector<string> split_string(string input_string) { |
| 52 | + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { |
| 53 | + return x == y and x == ' '; |
| 54 | + }); |
| 55 | + |
| 56 | + input_string.erase(new_end, input_string.end()); |
| 57 | + |
| 58 | + while (input_string[input_string.length() - 1] == ' ') { |
| 59 | + input_string.pop_back(); |
| 60 | + } |
| 61 | + |
| 62 | + vector<string> splits; |
| 63 | + char delimiter = ' '; |
| 64 | + |
| 65 | + size_t i = 0; |
| 66 | + size_t pos = input_string.find(delimiter); |
| 67 | + |
| 68 | + while (pos != string::npos) { |
| 69 | + splits.push_back(input_string.substr(i, pos - i)); |
| 70 | + |
| 71 | + i = pos + 1; |
| 72 | + pos = input_string.find(delimiter, i); |
| 73 | + } |
| 74 | + |
| 75 | + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); |
| 76 | + |
| 77 | + return splits; |
| 78 | +} |
0 commit comments