2150. Find All Lonely Numbers in the Array Medium
1/**
2 * [2150] Find All Lonely Numbers in the Array
3 *
4 * You are given an integer array nums. A number x is lonely when it appears only once, and no adjacent numbers (i.e. x + 1 and x - 1) appear in the array.
5 * Return all lonely numbers in nums. You may return the answer in any order.
6 *
7 * Example 1:
8 *
9 * Input: nums = [10,6,5,8]
10 * Output: [10,8]
11 * Explanation:
12 * - 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
13 * - 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
14 * - 5 is not a lonely number since 6 appears in nums and vice versa.
15 * Hence, the lonely numbers in nums are [10, 8].
16 * Note that [8, 10] may also be returned.
17 *
18 * Example 2:
19 *
20 * Input: nums = [1,3,5,3]
21 * Output: [1,5]
22 * Explanation:
23 * - 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
24 * - 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
25 * - 3 is not a lonely number since it appears twice.
26 * Hence, the lonely numbers in nums are [1, 5].
27 * Note that [5, 1] may also be returned.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length <= 10^5
33 * 0 <= nums[i] <= 10^6
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/
39// discuss: https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn find_lonely(nums: Vec<i32>) -> Vec<i32> {
45 vec![]
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2150() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.