Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
As for the explanation for initializing with the value that is an actual input rather than using some randomly picked number is that in case let's say we have
input as: 1 1 2 2 3 and my variable is initialized as
int result = 1;
It would actually lead to an incorrect output. So the sole reason for doing that was to make sure that my output stays correct irrelevant to the user input and I don't have to end up changing the value for the initialization; @mdfst @mdfst
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
As for the explanation for initializing with the value that is an actual input rather than using some randomly picked number is that in case let's say we have
input as: 1 1 2 2 3 and my variable is initialized as
int result = 1;
It would actually lead to an incorrect output. So the sole reason for doing that was to make sure that my output stays correct irrelevant to the user input and I don't have to end up changing the value for the initialization; @mdfst
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
As for the explanation for initializing with the value that is an actual input rather than using some randomly picked number is that in case let's say we have
input as: 1 1 2 2 3 and my variable is initialized as
int result = 1;
It would actually lead to an incorrect output. So the sole reason for doing that was to make sure that my output stays correct irrelevant to the user input and I don't have to end up changing the value for the initialization; @mdfst
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
As for the explanation for initializing with the value that is an actual input rather than using some randomly picked number is that in case let's say we have
input as: 1 1 2 2 3 and my variable is initialized as
int result = 1;
It would actually lead to an incorrect output. So the sole reason for doing that was to make sure that my output stays correct irrelevant to the user input and I don't have to end up changing the value for the initialization; @mdfst
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
As for the explanation for initializing with the value that is an actual input rather than using some randomly picked number is that in case let's say we have
input as: 1 1 2 2 3 and my variable is initialized as
int result = 1;
It would actually lead to an incorrect output. So the sole reason for doing that was to make sure that my output stays correct irrelevant to the user input and I don't have to end up changing the value for the initialization; @mdfst
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
Improving Margus's Code, initialize the result with the first value; Here's the code snippet:
int result = -1;
size_t size = 0;
std::cin >> size;
int input = 0;
for(int i = 0; i < size; i++) {
std::cin >> input;
if( i == 0 ) {
result = input;
}
else {
result ^= input;
}
if(result == -1) {
std::cout << "No number is lonely" << std::endl;
}
else {
std::cout << "Lonely Number: " << result << std::endl;
}
Space Complexity : O(1)
Time Complexity : O(n), where n is the size
LINK for different version of the function (LonelyInteger)
As @mdfst suggested:
int LonelyInteger(vector < int > a, int overrider) {
int tmp = a[0];
for(int i = 1; i < a.size(); i++) {
tmp ^= a[i];
}
return tmp;
}