Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Lonely Integer problem solution

YASH PAL, 31 July 2024

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.

HackerRank Lonely Integer problem solution

Problem solution in Python.

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))

Problem solution in Java.

import java.io.*;
import java.util.*;

class Bismillah {

  public static void main(String[] args){
    Scanner in = new Scanner(System.in);
	
	
	int n = in.nextInt();
	int[] ai = new int[n];
	int i, j;
	int x = 0;
	boolean state;
	
	
	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++.

#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.

#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;
}

Algorithms coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes