💡
Seb's Whiteboard
  • 👨‍💻Welcome, I'm Sebastien St Vil
  • Extras
    • Gradient Descent
    • How I learned Java
    • Machine Learning by Andrew Ng
  • Projects
    • 📉Backtest Equity Trading with SMA Strategy
    • Wellington GA Lab
    • Stock analysis
    • 📈Time Series Regression-based Trading Strategy
  • Arrays & Strings
    • Best Time to Buy and Sell Stock II
    • Online Stock Span
    • Implement strStr()
    • 2Sum
    • 3Sum
    • 3Sum Closest
    • 4Sum II
    • Set Matrix Zeroes
    • Group Anagrams
    • Longest Substring Without Repeating Characters
    • Remove Duplicates from Sorted Array
    • Move Zeroes
    • Valid Sudoku
    • Rotate Image
    • First Unique Character in a String
    • Design a Circular Queue
    • Longest Common Prefix
  • Binary Tree
  • Second Minimum Node In a Binary Tree (671)
  • Design
  • LRU Cache
  • Min Stack (155)
  • Sorting & Searching
    • Merge Sorted Array (88)
    • First Bad Version
  • Math
    • Power of Three (326)
    • Power of Two (231)
    • Count Prime (204)
    • Roman to Integer (13)
    • Fizz Buzz (412)
    • Count-and-Say
  • Dynamic Programming
    • Pascal's Triangle (118)
  • Linked List
    • Copy List with Random Pointer
    • Remove Nth Node From End of List
    • Remove Duplicated from Sorted List II
  • Tips
    • Finding dups
  • Sliding Window
    • Subarray Product Less Than K
Powered by GitBook
On this page

Was this helpful?

  1. Tips

Finding dups

Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

There is only one repeated number in nums, return this repeated number.

Input: nums = [3,1,3,4,2]
Output: 3

Brute Force: We consider each number from 1 to n, and traverse over the whole nums array to check if the current number occurs twice in nums.

Honorable mentions: Gauss Formula and Tortoise and Hare technique

When encountering problems that involving finding the duplicates of an array of some sort, one of the most useful and elegant techniques for a O(1) Space Complexity and O(N) time is to use the numbers in the array as indices to parcour the array and to subsequently set them to negative thereby indicating that they've been visited before hence that number being a duplicate.

def findDuplicate(self, nums: List[int]) -> int:
        n = len(nums)
        dup = 0
        
        for x in nums:
            index = abs(x)-1
            if nums[index] > 0:
                nums[index] *= -1
            else:
                dup = abs(x)
        
        return dup

Conversely, as seen in the below code, if the identified number is not negative, this signify that it has not been visited before due to the number being missing. Reminder to add 1 as the method to arrive to the element requires the index to be substracted by 1.

def findErrorNums(self, nums: List[int]) -> List[int]:
        n = len(nums)
        missing, dup = 0, 0
        
        for x in nums:
            index = abs(x) - 1
            if nums[index] > 0:
                nums[index] *= -1
            else:
                dup = abs(x)
        
        for i in range(n):
            if nums[i] > 0:
                missing = i + 1
        
        return [dup,missing]
PreviousRemove Duplicated from Sorted List IINextSubarray Product Less Than K

Last updated 4 years ago

Was this helpful?