|
| 1 | +# 🔥 3 Solutions 🔥 || Simple Fast and Easy || with Explanation 😈 |
| 2 | + |
| 3 | +## Brute Force |
| 4 | + |
| 5 | +The provided code aims to find the single number in a list that occurs only once, while all other numbers occur twice. |
| 6 | + |
| 7 | +### Algorithm Explanation |
| 8 | + |
| 9 | +1. Initialize an empty HashMap called `map` to store the frequency count of each number. |
| 10 | +2. Iterate over each element `x` in the input list `nums`. |
| 11 | +3. Increment the frequency count of `x` in the `map` by 1. If `x` is not present in the `map`, initialize its count as 0 before incrementing. |
| 12 | +4. After counting the frequencies, iterate over the entries of the `map` using `map.entries`. |
| 13 | +5. For each entry, check if the value is equal to 1. If it is, return the corresponding key (the single number). |
| 14 | +6. If no single number is found, return -1. |
| 15 | + |
| 16 | +### Space Complexity |
| 17 | + |
| 18 | +The space complexity is O(n), where n is the number of elements in the input list `nums`. This is because additional space is required to store the frequencies of the numbers in a HashMap. |
| 19 | + |
| 20 | +### Time Complexity |
| 21 | + |
| 22 | +The time complexity is O(n), where n is the number of elements in the input list `nums`. The code iterates over the input list once to count the frequencies, and then iterates over the entries of the `map`. However, since the number of unique elements in `nums` is represented as `m`, the worst-case time complexity can be expressed as O(n + m) = O(n). In the worst case, where all elements are unique, `m` is equal to `n`. |
| 23 | + |
| 24 | +```dart |
| 25 | +import 'dart:collection'; |
| 26 | +
|
| 27 | +class Solution { |
| 28 | + int singleNumber(List<int> nums) { |
| 29 | + HashMap<int, int> map = HashMap<int, int>(); |
| 30 | +
|
| 31 | + for (int x in nums) { |
| 32 | + map[x] = (map[x] ?? 0) + 1; |
| 33 | + } |
| 34 | +
|
| 35 | + for (MapEntry entry in map.entries) { |
| 36 | + if (entry.value == 1) { |
| 37 | + return entry.key; |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + return -1; |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Bit- Manipulation |
| 47 | + |
| 48 | +### Algorithm Explanation |
| 49 | + |
| 50 | +The given code aims to find the single number in a list that occurs only once, while all other numbers occur three times. The code uses bitwise operations to solve the problem efficiently. |
| 51 | + |
| 52 | +1. Initialize a variable `ans` as 0 to store the result. |
| 53 | +2. Iterate `i` from 0 to 31 (representing the 32 bits of an integer). |
| 54 | +3. Initialize a variable `sum` as 0 to keep track of the sum of the i-th bit for all numbers. |
| 55 | +4. Iterate over each number in the input list `nums`. |
| 56 | + - Right shift the number by `i` bits and perform a bitwise AND operation with 1 to get the i-th bit. |
| 57 | + - Add the i-th bit to `sum`. |
| 58 | +5. Take the modulus of `sum` with 3 to handle numbers occurring three times. |
| 59 | +6. Use the bitwise OR operation to set the i-th bit of `ans` based on `sum`. |
| 60 | +7. After the loop, check if the sign bit (bit 31) of `ans` is set (negative number check). |
| 61 | + - If the sign bit is set, convert `ans` to a negative number by performing necessary bitwise operations. |
| 62 | +8. Return the final result `ans`. |
| 63 | + |
| 64 | +### Space Complexity |
| 65 | + |
| 66 | +The space complexity of the code is O(1) since it uses a constant amount of additional space. The space required does not grow with the size of the input list. |
| 67 | + |
| 68 | +### Time Complexity |
| 69 | + |
| 70 | +The time complexity of the code is O(n), where n is the number of elements in the input list `nums`. The code consists of two nested loops: one loop iterating `i` from 0 to 31, and another loop iterating over each number in `nums`. However, since the number of bits being processed (32) and the number of elements in `nums` do not depend on each other, the time complexity remains linear. Therefore, the code performs an efficient linear scan of the input list to find the single number. |
| 71 | + |
| 72 | +Overall, the code achieves a linear time complexity and constant space complexity, making it an efficient solution for finding the single number in the given scenario. |
| 73 | + |
| 74 | +```dart |
| 75 | +class Solution { |
| 76 | + int singleNumber(List<int> nums) { |
| 77 | + int ans = 0; |
| 78 | +
|
| 79 | + for (int i = 0; i < 32; ++i) { |
| 80 | + int sum = 0; |
| 81 | + for (final int number in nums) { |
| 82 | + sum += (number >> i) & 1; |
| 83 | + } |
| 84 | + sum %= 3; |
| 85 | + ans |= (sum << i); |
| 86 | + } |
| 87 | +
|
| 88 | + // Handle negative numbers |
| 89 | + if ((ans & (1 << 31)) != 0) { |
| 90 | + ans = -(1 << 31) | (ans & ((1 << 31) - 1)); |
| 91 | + } |
| 92 | +
|
| 93 | + return ans; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Magical XOR |
| 99 | + |
| 100 | +### Algorithm Explanation |
| 101 | + |
| 102 | +The given code aims to find the single number in a list that occurs only once, while all other numbers occur exactly twice. The code uses bitwise operations to efficiently solve the problem. |
| 103 | + |
| 104 | +1. Initialize two variables `ones` and `twos` to 0. These variables are used to keep track of the bits that appear once and twice, respectively. |
| 105 | +2. Iterate over each number in the input list `nums`. |
| 106 | + - XOR (`^`) the current number with the bitwise complement of `twos` and store the result in `ones`. This operation will set the bits that appear once in `ones`. |
| 107 | + - XOR (`^`) the current number with the bitwise complement of `ones` and store the result in `twos`. This operation will set the bits that appear twice in `twos`. |
| 108 | +3. Repeat the loop for all numbers in `nums`, and after the loop, the variable `ones` will contain the single number that appears only once. |
| 109 | + |
| 110 | +### Space Complexity |
| 111 | + |
| 112 | +The space complexity of the code is O(1) since it uses a constant amount of additional space. The space required remains the same regardless of the size of the input list. |
| 113 | + |
| 114 | +### Time Complexity |
| 115 | + |
| 116 | +The time complexity of the code is O(n), where n is the number of elements in the input list `nums`. The code iterates over each number once, performing constant time operations (bitwise XOR and AND) for each number. The time complexity scales linearly with the size of the input list. |
| 117 | + |
| 118 | +Overall, the code achieves an efficient linear time complexity and constant space complexity, making it an optimal solution for finding the single number in the given scenario. It leverages bitwise operations to efficiently keep track of the numbers that occur once and twice, providing an elegant solution with minimal space usage. |
| 119 | + |
| 120 | +```dart |
| 121 | +class Solution { |
| 122 | + int singleNumber(List<int> nums) { |
| 123 | + int ones = 0; |
| 124 | + int twos = 0; |
| 125 | +
|
| 126 | + for (int i = 0; i < nums.length; i++) { |
| 127 | + final int number = nums[i]; |
| 128 | + ones ^= (number & ~twos); |
| 129 | + twos ^= (number & ~ones); |
| 130 | + } |
| 131 | +
|
| 132 | + return ones; |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments