Abstract
The Lonely Integer shell problem from HackerRank.
Lonely Integer#
Overview#
There are
Input Format#
The first line of the input contains an integer
Constraints#
Output Format#
Output , the number that occurs only once.
Sample Input:1#
1
1
Sample Output:1#
1
Sample Input:2#
3
1 1 2
Sample Output:2#
2
Sample Input:3#
5
0 0 1 2 1
Sample Output:3#
2
Explanation#
In the first input, we see only one element (1) and that element is the answer.
In the second input, we see three elements; 1 occurs at two places and 2 only once. Thus, the answer is 2.
In the third input, we see five elements. 1 and 0 occur twice. The element that occurs only once is 2.
Solution#
#!/bin/bash
# shellcheck disable=SC2162,SC2034
# Read number of integers
read n
# Read array of integers
read -a int_array
# Init result for XOR
result=0
for i in "${int_array[@]}"; do
result=$(("$result" ^ "$i"))
done
echo "$result"