Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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

Updated the initial value based on the constraints and provided an explanation
Source Link

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

Updated the initial value based on the constraints.
Source Link

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;
}
Updated the initial value based on the constraints.
Source Link
Loading
Source Link
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /