Source : https://www.hackerrank.com/challenges/lonely-integer
You will be given an array of integers. All of the integers except one occur twice. That one is unique in the array.
Given an array of integers, find and print the unique element.
For example, , the unique element is .
Function Description
Complete the lonelyinteger function in the editor below. It should return the integer which occurs only once in the input array.
lonelyinteger has the following parameter(s):
- a: an array of integers
Input Format
The first line contains a single integer, , denoting the number of integers in the array.
The second line contains space-separated integers describing the values in .
Constraints
- It is guaranteed that is an odd number and that there is one unique element.
- , where .
Output Format
Print the unique integer in the array.
Sample Input 0
11
Sample Output 0
1
Explanation 0
There is only one element in the array, thus it is unique.
Sample Input 1
31 1 2
Sample Output 1
2
Explanation 1
We have two 's, and is unique.
Sample Input 2
50 0 1 2 1
Sample Output 2
2
Explanation 2
We have two 's, two 's, and one . is unique.
Source : https://www.hackerrank.com/challenges/lonely-integer
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
// O(n) runtime. O(1) space. Uses XOR. Keep in mind: | |
// 1) x ^ x = 0 | |
// 2) x ^ 0 = x | |
// 3) XOR is commutative and associative | |
public class Solution { | |
public static void main(String[] args) { | |
/* Save input */ | |
Scanner scan = new Scanner(System.in); | |
int n = scan.nextInt(); | |
int [] array = new int[n]; | |
for(int i = 0; i < n; i++){ | |
array[i] = scan.nextInt(); | |
} | |
scan.close(); | |
System.out.println(lonelyInteger(array)); | |
} | |
/* XORs all numbers in array together */ | |
public static int lonelyInteger(int [] array) { | |
int val = 0; | |
for (int num : array) { | |
val = val ^ num; // ^ is XOR operator | |
} | |
return val; | |
} | |
} |
No comments:
Post a Comment