lonely-integer - HackerRank Solution

lonely-integer - HackerRank Solution
lonely-integer - HackerRank Solution

Problem Statement

Given an array of integers, where all elements but one occur twice, find the
unique element.

Example :
a = [1, 2, 3, 4, 3, 2, 1]
The unique element is 4.

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

Input Format
The first line contains a single integer, n, the number of integers in the array.
The second line contains n space-separated integers that describe the values
in a.


Constraints

n 100
it is guaranteed that n is an odd number and that there is one unique element.
0 <= a[i] <= 100, where 0<=i<n

sample Input 0 :

1
1

sample output 0 :

1

Explanation 0 :

There is only one element in the array, thus it is unique.

sample Input 1 :

3
1 1 2

sample output 1 :

2

Explanation 1 :

We have two 1s, and 2 is unique.

Sample Input 2 :

5
0 0 1 2 1

Sample Output 2 :

2

Explanation 2 :

We have two 0's, two 1's, and one 2. 2 is unique.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'lonelyinteger' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY a as parameter.
     */
//-----------lonely-integer - HackerRank Solution start-----------
    public static int lonelyinteger(List<Integer> a) {
    // Write your code here
    
        int output = a.get(0);
        for(int i=1;i<a.size();i++){
            output = output^a.get(i);
        }
        return output;
    }
//-----------lonely-integer - HackerRank Solution end ----------------
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.lonelyinteger(a);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}



Disclaimer :-
The above whole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority, if any of the query regarding this post or website fill the following contact form.Thank you.
Previous Post
No Comment
Add Comment
comment url