0% found this document useful (0 votes)
229 views4 pages

HackerRank Lonely Integer Problem Solution

The document discusses solutions to the HackerRank Lonely Integer problem in Python, Java, C++ and C. The problem involves finding the unique element in an array where all other elements occur twice. The Python solution uses sorting and recursion. The Java solution uses nested for loops to check for duplicate elements. The C++ solution uses XOR to find the unique element. The C solution also uses XOR and has a similar structure to the C++ solution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views4 pages

HackerRank Lonely Integer Problem Solution

The document discusses solutions to the HackerRank Lonely Integer problem in Python, Java, C++ and C. The problem involves finding the unique element in an array where all other elements occur twice. The Python solution uses sorting and recursion. The Java solution uses nested for loops to check for duplicate elements. The C++ solution uses XOR to find the unique element. The C solution also uses XOR and has a similar structure to the C++ solution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Home  coding problems  HackerRank Lonely Integer problem solution

HackerRank
CLOSE ADS Lonely Integer problem solution CLOSE ADS

 YASH PAL  July 27, 2021

In this HackerRank Lonely Integer problem solution, we have Given an array of integers,
where all elements but one occur twice, find the unique element.

12% OFF 19% OFF 24% OFF

Search

₹93 ₹718 ₹93


10% OFF 15% OFF 15% OFF

₹224 ₹28 ₹72

Subscribe To Channel

Programmingoneonone

YouTube 797

12% OFF 19% OFF 24% OFF

Learn DSA For Free

10% OFF 15% OFF 15% OFF

12% OFF 6% OFF 24% OFF 15% OFF


₹93 ₹34 ₹93 ₹72
Crafted with  by TemplatesYard | Distributed by Blogger

Problem
CLOSE ADSsolution in Python. CLOSE ADS

Code
def lonelyinteger(a):
a = sorted(a)
if len(a) < 3:
return a[0]
elif a[0] != a[1]:
return a[0]
else:
return lonelyinteger(a[2:])

if __name__ == '__main__':
a = int(input())
b = map(int, input().strip().split(" "))
print(lonelyinteger(b))

Most Popular Content


Problem solution in Java.
HackerRank Mini-Max Sum
Code problem solution
 March 23, 2021
import java.io.*;
import java.util.*;
HackerRank Plus Minus
problem solution
class Bismillah {  March 23, 2021

public static void main(String[] args){ HackerRank Time Conversion


problem solution
Scanner in = new Scanner(System.in);
 March 23, 2021

HackerRank Diagonal
int n = in.nextInt(); Difference problem solution
int[] ai = new int[n];  March 23, 2021

int i, j;
HackerRank Simple Array Sum
int x = 0;
problem solution
boolean state;  March 23, 2021

for (i = 0; i < n; i++) {


ai[i] = in.nextInt();
}

for (i = 0; i < n; i++) {


state = true;
for (j = 0; j < i; j++) {
if (ai[i] == ai[j]) {
j = i;
state = false;
} 
}
if (state == true) {
for (j = i+1; j < n; j++) {
if (ai[i] == ai[j]) {
j = n;
state = false;
}
}
if (state == true) {
x = i;
i = n;
}
}
}

System.out.println(ai[x]);
}
}

Problem solution in C++.


Code
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int lonelyinteger(vector < int > a) {
int res = 0;
for(int i = 0;i<(int)a.size();i++)
res ^= a[i];
return res;
}
int main() {
int res;

int _a_size;
cin >> _a_size;
cin.ignore (std::numeric_limits<std::streamsize>::max(),
'\n');
vector<int> _a;
int _a_item;
for(int _a_i=0; _a_i<_a_size; _a_i++) {
cin >> _a_item;
_a.push_back(_a_item);
}

res = lonelyinteger(_a);
cout << res;

return 0;
}

Problem solution in C.
Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
int lonelyinteger(int a_size, int* a) {
int res = 0;
for(int i=0; i<a_size; i++){
res = res^a[i];
}
return res;

}
int main() {
int res;

int _a_size, _a_i;


scanf("%d", &_a_size);
int _a[_a_size];
for(_a_i = 0; _a_i < _a_size; _a_i++) {
int _a_item;
scanf("%d", &_a_item);

_a[_a_i] = _a_item;
}

res = lonelyinteger(_a_size, _a);


printf("%d", res);

return 0;
}

You might also like