Find All Duplicates In An Array

To solve this coding challenge, we need to identify the integers in an array that appear exactly twice. The constraints specify that we need an algorithm with a time complexity of O(n) and constant extra space. Let's break down the problem and the solution step by step.

Explanation

The given integer array
nums
consists of integers ranging from 1 to n and each integer appears either once or twice. The goal is to return an array of all the integers that appear twice. Here's how we can achieve the solution with the given constraints:
  1. Utilize Indexing for Marking: We can use the array itself to keep track of which numbers have been seen once. By marking visited numbers using negative signs, we can identify duplicates.
  2. Marking Steps:
    • Iterate through each number in the array.
    • For each number, calculate its corresponding index by taking the absolute value of the number and subtracting one (since the numbers are 1-based, and array indices are 0-based).
    • Check if the number at this index is negative. If it is, it means the number corresponding to this index has already been visited, indicating a duplicate.
    • If the number at this index is not negative, mark the number at this index as visited by making it negative.
  3. Collect the Duplicates: When we encounter a number that points to an already marked index (negative value), we add it to the result list as it signifies a duplicate.
  4. This approach ensures that we are only using constant extra space (aside from the input and output) and that we are processing each element of the array once, making the time complexity O(n).

    Step-by-Step Explanation / Detailed Steps in Pseudocode

  5. Initialize an empty result list.
  6. Iterate over each number in the input array (nums).
  7. For each number, compute the corresponding index in the array:
    • Calculate the index as the absolute value of the number minus one.
  8. Check the sign of the number at the computed index:
    • If the number at this index is negative, this means the current number is a duplicate.
    • If the number at this index is positive, mark it as visited by making it negative.
  9. If a duplicate is found (step 4's negative check), add the absolute value of the current number to the result list.
  10. Return the result list containing all duplicates.
Now, let's write the pseudocode with comments integrated for each of these steps:
                                            
# Function to find duplicates in an array
function findDuplicates(nums):
    # Initialize an empty list to store duplicates
    duplicates = []
    
    # Iterate through each number in the array
    for each number in nums:
        # Calculate the corresponding index (convert to zero-based index)
        index = absolute value of number - 1
        
        # Check if the number at this index is negative
        if nums[index] < 0:
            # If so, it is a duplicate and add its absolute value to the result list
            append absolute value of number to duplicates
        else:
            # Otherwise, mark the number at this index as visited (turn it negative)
            nums[index] = -nums[index]
    
    # Return the list of duplicates found
    return duplicates

                                        
By following this pseudocode, you will achieve the desired solution with the given constraints: O(n) time complexity and constant extra space. This detailed step-by-step explanation ensures clarity on each action taken to derive the result while maintaining the efficiency of the algorithm.