Article Directory
1. Description
A number that appears only once
Given an array of non-empty integers, each element appears twice except for one element. Find the element that appears only once.
Explanation:
Your algorithm should have linear time complexity. Can you do this without using extra space?
2. Example
Input: [2,2,1]
Output: 1
Input: [4,1,2,1,2]
Output: 4
3. Analysis
Method 1: Use extra space to achieve.
The problem of counting the most/least occurrences of elements in an array naturally comes to mind.
The value of the element is used as the key of the dictionary, and the number of occurrences of the element is used as the value to form a (key, value) pair.
This method first needs to traverse the array once, and the time complexity is O (n) O(n)O ( n ) ; Then you need to traverse the dictionary once, the size of the dictionary is(⌊ n / 2 ⌋ + 1) (\lfloor n / 2\rfloor + 1)(⌊n/2⌋+1 ) , so the time complexity isO (n) O(n)O ( n ) . The total time complexity isO (n) O(n)O ( n ) , which satisfies the requirement of "linear time complexity".
Method 2: Do not use additional space to achieve.
The title specifically indicates that except for a certain element that appears only once, every other element appears twice . Does this sentence have any special meaning?
Suppose a number is 5, it appears twice, that is, two 5s, let the AND (&) operation between them.
Calculation | Binary representation |
---|---|
. | 0101(5) |
& (And operation) | 0101(5) |
result | 0101(5) |
Nothing seems to have changed! It has nothing to do with finding elements that only appear once.
Try the OR (|) operation again.
Calculation | Binary representation |
---|---|
. | 0101(5) |
| (Or operation) | 0101(5) |
result | 0101(5) |
It seems that something has changed! It has nothing to do with finding elements that only appear once.
Try the exclusive OR (^) operation again.
Calculation | Binary representation |
---|---|
. | 0101(5) |
^ (Exclusive OR operation) | 0101(5) |
result | 0000(0) |
It's a bit enlightening.
If a number appears twice, the result of the exclusive OR operation is 0.
If a number appears only once, then this number and 0 (the rest of the numbers appear twice, the result of the XOR operation is 0) XOR operation results are as follows
Calculation | Binary representation |
---|---|
. | 0100(4) |
^ (Exclusive OR operation) | 0000(0) |
result | 0100(4) |
The result is the number itself that appears only once.
From this, draw the conclusion:
The number that appears only once in the array is equal to the result of the exclusive OR operation of all the numbers in the array.
Traverse the array once, the time complexity is O (n) O(n)O ( n ) ; XOR operation is a bit operation, and the time complexity can be considered asO (1) O(1)O ( 1 ) .
So the total time complexity isO (n) O(n)O ( n ) , which satisfies the requirement of "linear time complexity", and is more concise and efficient than method one!
4. Code
Method one :
class Solution
{
public int singleNumber(int[] nums)
{
// HashMap 作为字典使用
Map<Integer, Integer> pair = new HashMap<>();
// 统计数字(键)出现的次数(值)
for(int element: nums)
{
// 如果数字在字典中出现过,那么其出现次数 + 1
if(pair.containsKey(element))
{
pair.put(element, pair.get(element) + 1);
}
// 否则, 其出现次数置为1
else
{
pair.put(element, 1);
}
}
int only = 0;
// 遍历得到字典中值为1的键,也即是只出现一次的数字
for(int key: pair.keySet())
{
if(pair.get(key) == 1)
{
only = key;
break;
}
}
return only;
}
}
Method two :
class Solution
{
public int singleNumber(int[] nums)
{
// bitExclusiveOr 保存每一步异或运算的结果
int bitExclusiveOr = 0;
// 将数组中所有数字进行异或运算
for(int element : nums)
{
bitExclusiveOr ^= element;
}
// 数组中只出现一次的数字就等于异或运算的结果
return bitExclusiveOr;
}
}
5. Verification
Method one :
Method two :