-
Notifications
You must be signed in to change notification settings - Fork 31
/
0136-single-number.rs
39 lines (32 loc) · 1.26 KB
/
0136-single-number.rs
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
/*
Problem: LeetCode 136 - Single Number
Key Idea:
The key idea is to use the bitwise XOR operation to find the single number in the array. XOR has the property that it returns 1 for each bit position where the two input bits are different and 0 for each bit position where they are the same. Therefore, when we XOR all the elements in the array, all the elements that appear twice will cancel each other out, leaving only the single element.
Approach:
1. Initialize a variable 'result' to 0. This variable will store the single number.
2. Iterate through each element 'num' in the array.
3. Update 'result' by XOR-ing it with 'num'.
4. After the loop, 'result' will contain the single number that appears only once in the array.
5. Return 'result'.
Time Complexity:
O(n), where 'n' is the number of elements in the array. We iterate through the array once.
Space Complexity:
O(1), as we use a constant amount of extra space.
*/
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let mut result = 0;
for num in nums {
result ^= num;
}
result
}
}
/*
// One liner
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
nums.iter().fold(0, |acc, x| acc ^ x)
}
}
*/