From a0b1d1ecbf180a9a29c1a1d18c6dbd65e8751a03 Mon Sep 17 00:00:00 2001 From: Srishti Guleria Date: Mon, 26 Oct 2020 17:03:50 +0530 Subject: [PATCH] Hacktoberfeast Contribution by Srishti Guleria --- Lonely Integer.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Lonely Integer.cpp diff --git a/Lonely Integer.cpp b/Lonely Integer.cpp new file mode 100644 index 0000000..5dc711e --- /dev/null +++ b/Lonely Integer.cpp @@ -0,0 +1,100 @@ +/* Hackerrank Problem: You will be given an array of integers. All of the integers except one occur twice. That one is unique in the array. + +Given an array of integers, find and print the unique element. + +For example, , the unique element is . + +Function Description + +Complete the lonelyinteger function in the editor below. It should return the integer which occurs only once in the input array. + +lonelyinteger has the following parameter(s): + +a: an array of integers +Input Format + +The first line contains a single integer, , denoting the number of integers in the array. +The second line contains space-separated integers describing the values in . + +Constraints + +It is guaranteed that is an odd number and that there is one unique element. +, where . +Output Format + +Print the unique integer in the array.*/ + +#include + +using namespace std; + +vector split_string(string); +int lonelyinteger(vector a) { +int c=0; +int uni; +for(int i=0;i> n; + cin.ignore(numeric_limits::max(), '\n'); + + string a_temp_temp; + getline(cin, a_temp_temp); + + vector a_temp = split_string(a_temp_temp); + + vector a(n); + + for (int i = 0; i < n; i++) { + int a_item = stoi(a_temp[i]); + + a[i] = a_item; + } + + int result = lonelyinteger(a); + + fout << result << "\n"; + + fout.close(); + + return 0; +} + +vector split_string(string input_string) { + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { + return x == y and x == ' '; + }); + + input_string.erase(new_end, input_string.end()); + + while (input_string[input_string.length() - 1] == ' ') { + input_string.pop_back(); + } + + vector splits; + char delimiter = ' '; + + size_t i = 0; + size_t pos = input_string.find(delimiter); + + while (pos != string::npos) { + splits.push_back(input_string.substr(i, pos - i)); + + i = pos + 1; + pos = input_string.find(delimiter, i); + } + + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); + + return splits; +} +