Just a runnable solution
\nimpl Solution {\n    pub fn max_selected_elements(nums: Vec<i32>) -> i32 {\n        let mut dp = std::collections::HashMap::new();\n        let mut nums = num
```\nimpl Solution {\n    pub fn max_selected_elements(nums: Vec<i32>) -> i32 {\n        let mut dp = std::collections::HashMap::new();\n        let mut nums = nums;\n        nums.sort();\n        let mut res = 0;\n        for a in nums {\n            let dp_a = *dp.get(&(a)).unwrap_or(&0);\n            *dp.entry(a + 1).or_insert(0) = dp_a + 1;\n            res = res.max(dp_a + 1);\n            let dp_a_1 = *dp.get(&(a - 1)).unwrap_or(&0);\n            *dp.entry(a).or_insert(0) = dp_a_1 + 1;\n            res = res.max(dp_a_1 + 1);\n        }\n        res\n    }\n}\n```
['Rust']