Leetcode 136: Single Number
Given a non-empty array of integers where every element appears twice except for one element that appears once, find the single element that appears only once. The solution must have linear time complexity and use only constant space.
Problem
Approach
Steps
Complexity
Input: You are given an array of integers where all but one element appear twice. The array is non-empty and contains at least one element.
Example: nums = [5, 5, 3]
Constraints:
• 1 <= nums.length <= 3 * 10^4
• -3 * 10^4 <= nums[i] <= 3 * 10^4
Output: Return the element that appears only once in the array.
Example: Output: 3
Constraints:
• The array contains exactly one element that appears once.
Goal: Find the element that appears once by using XOR.
Steps:
• 1. Use the XOR operation to find the element that appears once. XORing the same number twice cancels it out, leaving only the unique element.
• 2. Initialize a variable to 0 and XOR it with each element in the array.
• 3. Return the result after the final XOR operation, which will be the single number.
Goal: The constraints ensure that the solution operates efficiently even for large arrays.
Steps:
• 1 <= nums.length <= 3 * 10^4
• -3 * 10^4 <= nums[i] <= 3 * 10^4
Assumptions:
• The array is guaranteed to contain one unique element and all other elements appear exactly twice.
• Input: nums = [5, 5, 3]
• Explanation: In this example, the number 3 appears once while 5 appears twice. The result is 3.
• Input: nums = [7, 9, 7, 3, 9]
• Explanation: In this case, 3 appears once, and all other numbers appear twice. The result is 3.
• Input: nums = [4]
• Explanation: Since there is only one number in the array, the result is that number itself, 4.
Approach: The approach to solving this problem is based on using XOR operation to find the unique element, which guarantees a linear time solution with constant space.
Observations:
• XOR can be used efficiently to identify the unique element, as XORing the same number twice cancels it out.
• We need to ensure that we process the array in linear time while using constant space.
Steps:
• 1. Initialize a variable to 0 to store the result.
• 2. Iterate through each element in the array and XOR it with the result.
• 3. After the loop, the result will contain the unique element.
Empty Inputs:
• If the array is empty, return an error or handle it as a constraint violation.
Large Inputs:
• The solution must handle arrays with up to 30,000 elements efficiently.
Special Values:
• Handle cases where the only element in the array is the unique element.
Constraints:
• Ensure that the solution works efficiently even for large input sizes and adheres to the time and space complexity requirements.
int singleNumber(vector<int>& nums) {
int x = 0;
for(int y: nums)
x ^= y;
return x;
}
1 : Function Definition
int singleNumber(vector<int>& nums) {
This is the function definition of `singleNumber`, which takes a vector of integers and returns the number that appears only once.
2 : Variable Initialization
int x = 0;
Initialize a variable `x` to 0. This will hold the result of XOR operations and ultimately the number that appears only once.
3 : Loop Iteration
for(int y: nums)
Start a loop to iterate through each element `y` in the `nums` array.
4 : Bitwise Operation
x ^= y;
Apply the XOR operation between `x` and the current element `y`. This operation ensures that pairs of the same number cancel each other out.
5 : Return Statement
return x;
Return the value of `x`, which holds the number that appeared only once after all XOR operations.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) as we traverse the array once to find the unique element.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is O(1) as we use only a constant amount of space for calculations.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus