20241012 finished.

This commit is contained in:
jackfiled 2024-10-12 13:36:57 +08:00
parent 4533063186
commit 84e76d7224
2 changed files with 44 additions and 1 deletions

View File

@ -260,4 +260,5 @@ mod p871_minimum_number_of_refueling_stops;
mod p1436_destination_city; mod p1436_destination_city;
mod p3171_find_subarray_with_bitwise_or_closest_to_k; mod p3171_find_subarray_with_bitwise_or_closest_to_k;
mod p3162_find_the_number_of_good_pairs_i; mod p3162_find_the_number_of_good_pairs_i;
mod p3164_find_the_number_of_good_pairs_ii; mod p3164_find_the_number_of_good_pairs_ii;
mod p3158_find_the_xor_of_numbers_which_appear_twice;

View File

@ -0,0 +1,42 @@
/**
* [3158] Find the XOR of Numbers Which Appear Twice
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn duplicate_numbers_xor(nums: Vec<i32>) -> i32 {
use std::collections::HashSet;
let mut set = HashSet::with_capacity(nums.len());
let mut result = 0;
for num in nums {
set.insert(num);
result ^= num;
}
for num in set {
result ^= num;
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3158() {
assert_eq!(1, Solution::duplicate_numbers_xor(vec![1, 2, 1, 3]));
assert_eq!(0, Solution::duplicate_numbers_xor(vec![1, 2, 3]));
assert_eq!(3, Solution::duplicate_numbers_xor(vec![1, 2, 2, 1]));
}
}