Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone

HackerRank Lonely Integer problem solution

YASH PAL, 31 July 202425 January 2026

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.

Function Description

Complete the lonelyinteger function in the editor below.

lonelyinteger has the following parameter(s):

  • int a[n]: an array of integers

Returns

  • int: the element that occurs only once
HackerRank Lonely Integer problem solution

HackerRank Lonely Integer 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))

Lonely Integer 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 AlgorithmsHackerRank

Post navigation

Previous post
Next post
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes