Single Number (easy)
Problem Statement
In a non-empty array of integers, every number appears twice except for one, find that single number.
Example 1:
Input: 1, 4, 2, 1, 3, 2, 3
Output: 4
Example 2:
Input: 7, 9, 7
Output: 9
Try it yourself
Try solving this question here:
class SingleNumber {public static int findSingleNumber(int[] arr) {// TODO: Write your code herereturn -1;}public static void main( String args[] ) {System.out.println(findSingleNumber(new int[]{1, 4, 2, 1, 3, 2, 3}));}}
Solution
One straight forward solution can be to use a HashMap kind of data structure and iterate through the input:
- If number is already present in HashMap, remove it.
- If number is not present in HashMap, add