Tech With Machines
Tech With Machines
  • 55
  • 5 545
Remove Duplicates From Sorted Array | Leetcode 26 | Data Structure Interview Question
The "Remove Duplicates from Sorted Array" problem is a common LeetCode question. Here's a detailed explanation and solution.
Problem Description
Given a sorted array nums, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be maintained. Return the new length of the array.
You must modify the input array in place using O(1) extra memory.
Example
Example 1:
Input:
nums = [1, 1, 2]
Output:
2, nums = [1, 2, _]
Explanation: After removing duplicates, the first two elements of nums are unique. The length is 2. The _ represents irrelevant values beyond the new length.
Example 2:
Input:
nums = [0,0,1,1,1,2,2,3,3,4]
Output:
5, nums = [0, 1, 2, 3, 4, ...]
Explanation: The first five elements are unique.
Solution: Two Pointer Technique
python
Copy code
def removeDuplicates(nums):
if not nums: # If the array is empty
return 0
# Initialize two pointers
i = 0 # Slow pointer
for j in range(1, len(nums)): # Fast pointer
if nums[j] != nums[i]: # Found a new unique element
i += 1
nums[i] = nums[j] # Update the position of the slow pointer
return i + 1 # Length of the unique array
Explanation of the Approach:
Use two pointers:
i: Tracks the position of the last unique element.
j: Iterates through the array.
When a new unique element is found (nums[j] != nums[i]), move i forward and update nums[i] to the value at nums[j].
At the end, the length of the modified array is i + 1.
Complexity:
Time Complexity: O(n), where n is the length of the array (single traversal).
Space Complexity: O(1), as no extra space is used.
Usage
You can test this function like so:
python
Copy code
nums = [0,0,1,1,1,2,2,3,3,4]
new_length = removeDuplicates(nums)
print(new_length) # Output: 5
print(nums[:new_length]) # Output: [0, 1, 2, 3, 4] @TechWithMachines #datastructures #algorithm #leetcode #array #removeduplicatesfromsortedarray
Переглядів: 11

Відео

Remove Element | Leetcode 27 | Data Structure Interview Question
Переглядів 9Місяць тому
The "Remove Element" problem on LeetCode is a common array manipulation problem. Here's a summary of the problem and its solution: Problem: You are given an integer array nums and an integer val. You need to modify the array in place such that all occurrences of val are removed. The relative order of the elements may be changed. You must return the new length of the array after removal. Do not ...
Running Sum of 1d Array | Leetcode 1480 | Data Structure Interview Question
Переглядів 29Місяць тому
The Running Sum of 1D Array is a common problem on LeetCode. The task is to compute the cumulative sum of an array, where the value at each index is the sum of all elements up to that index in the original array. Here’s an example of how to solve it: Problem Statement: Given an array nums, return an array result such that result[i] is the sum of nums[0] to nums[i]. Python Solution: python Copy ...
Merge Sorted Array | Leetcode 88 | Data Structure Interview Question
Переглядів 17Місяць тому
To merge two sorted arrays into one sorted array, you can use the merge step of the Merge Sort algorithm. Here's a simple Python implementation: Code Example: Merging Two Sorted Arrays python Copy code def merge_sorted_arrays(arr1, arr2): # Initialize pointers and result array i, j = 0, 0 merged_array = [] # Compare elements from both arrays and add the smallest to merged_array while i len(arr1...
Move Zeroes | Leetcode 283 | Data Structure Interview Question
Переглядів 12Місяць тому
The "Move Zeroes" problem on LeetCode is a classic array manipulation challenge. The task is to move all zeroes in an array to the end while keeping the relative order of the non-zero elements intact. Problem Statement: Given an integer array nums, move all 0s to the end while maintaining the relative order of the non-zero elements. Example: Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, ...
Squares Of Sorted Array | Leetcode 977 | Data Structure Interview Question
Переглядів 31Місяць тому
The problem "Squares of a Sorted Array" on LeetCode typically involves taking a sorted array of integers (which can include both negative and positive numbers), squaring each element, and then returning a new array where the squares are sorted in non-decreasing order. Approach Explanation Understand the Input: The array is sorted, meaning negative numbers (if present) are at the start and posit...
Majority Element | Leetcode 169 | Interview Question
Переглядів 12Місяць тому
The Majority Element problem on LeetCode asks you to find the element that appears more than ⌊n / 2⌋ times in a given array, where n is the size of the array. Here's a conceptual explanation without code: Key Insights: Definition of Majority Element: The majority element is guaranteed to appear more than half the time in the array. This means if we sort the array, the majority element will alwa...
Buy And Sell stock | Leetcode 121 | Interview Question
Переглядів 49Місяць тому
The "Buy and Sell Stock" problem is a classic algorithmic question found on LeetCode. There are several variations of this problem, each with its unique constraints and requirements. Here's a brief summary of the most common variations and their solutions: 1. Best Time to Buy and Sell Stock Problem: You are given an array prices where prices[i] is the price of a given stock on the ith day. You ...
Two Sum | Data Structures And Algorithm | Interview Question
Переглядів 9Місяць тому
The Two Sum problem is a classic question in data structures and algorithms, often used in coding interviews to assess problem-solving and optimization skills. Here's a detailed breakdown: Problem Statement Given an array of integers nums and a target integer target, return the indices of the two numbers that add up to the target. Example plaintext Copy code Input: nums = [2, 7, 11, 15], target...
RANK WINDOW FUNCTION / DATA ENGINEERING INTERVIEW QUESTION
Переглядів 264 місяці тому
WHAT IS RANK WINDOW FUNCTION The RANK window function in SQL is used to assign a unique rank to each row within a partition of a result set. It ranks rows based on the values in one or more columns. The ranking is determined by the order specified in the ORDER BY clause. Here’s a breakdown of the RANK window function: Syntax: sql RANK() OVER ([PARTITION BY column1, column2, ...] ORDER BY column...
SQL Window Function - Row Number / DATA ENGINEERING INTERVIEW QUESTION .
Переглядів 424 місяці тому
In this video, we dive deep into the powerful SQL ROW_NUMBER window function. Whether you're a beginner or an experienced developer looking to sharpen your SQL skills, this tutorial will help you understand how ROW_NUMBER works and how it can be used to solve real-world problems. 🔍 What You'll Learn: What is the ROW_NUMBER function? How to assign row numbers to partitions of data. Hands-on exam...
AIRFLOW BRANCH PYTHON OPERATOR
Переглядів 684 місяці тому
The BranchPythonOperator in Apache Airflow allows you to conditionally control the flow of execution in a Directed Acyclic Graph (DAG). It is useful when you want to create workflows where tasks are executed based on dynamic conditions. How BranchPythonOperator Works The operator decides which downstream task(s) to execute next by returning the task_id or a list of task_ids of the chosen branch...
Speculative Execution In Spark - Most common Spark Interview Question
Переглядів 1815 місяців тому
Welcome to our latest video on Apache Spark! 🚀 In this deep dive, we’ll unravel the concept of speculative execution and explore how it can significantly enhance the performance and reliability of your Spark applications. 🔍 What You’ll Learn: Introduction to Speculative Execution: Understand what speculative execution is and why it’s crucial for big data processing. How It Works: Discover the u...
MASTERING THE TRANSITION : SENIOR ENGINEER TO TECH LEAD
Переглядів 276 місяців тому
How to become a Successful Tech Lead : A Guide for Senior Engineers #techjobs #hiring #itjobs #itjobs2024 #jobs #jobsearch #jobseekers #tech #recruitment #technology #job #career #careers #hiringnow #techcareers #programming #nowhiring #it #womenintech #womenintechnology #recruitment #recruiting #technews #developer #coding #jobopportunity #remotejobs #remotework #careergoals #softwarejobs #sof...
PYTHON : Async Context Managers
Переглядів 2726 місяців тому
CONTEXT MANAGERS : A context manager in python is a construct that provides a way to allocate and release resources precisely when you want to. ASYNC OVERVIEW : Async context managers are a special type of context manager designed to work in asynchronous code. They use async with statements and allow you to set up and tear down resources in an asynchronous manner. #python #async #asyncawait #as...
KUBERNETES EXPLAINED IN 5 MINUTES | K8S ARCHITECTURE
Переглядів 387 місяців тому
KUBERNETES EXPLAINED IN 5 MINUTES | K8S ARCHITECTURE
DOCKER ARCHITECTURE | HOW IT WORKS ?
Переглядів 268 місяців тому
DOCKER ARCHITECTURE | HOW IT WORKS ?
WHAT IS DOCKER ? CONTAINERIZATION EXPLAINED
Переглядів 1458 місяців тому
WHAT IS DOCKER ? CONTAINERIZATION EXPLAINED
CATALYST OPTIMIZER | SPARK INTERVIEW QUESTION
Переглядів 6848 місяців тому
CATALYST OPTIMIZER | SPARK INTERVIEW QUESTION
DATA SKEWNESS | SPARK INTERVIEW QUESTIONS
Переглядів 1738 місяців тому
DATA SKEWNESS | SPARK INTERVIEW QUESTIONS
Asyncio in python - Full Tutorial
Переглядів 358 місяців тому
Asyncio in python - Full Tutorial
PYTHON ASYNCHRONOUS PROGRAMMING
Переглядів 318 місяців тому
PYTHON ASYNCHRONOUS PROGRAMMING
AIRFLOW DAG : LET'S CREATE OUR FIRST DAG
Переглядів 3479 місяців тому
AIRFLOW DAG : LET'S CREATE OUR FIRST DAG
LEARN APACHE AIRFLOW IN 11 MINUTES | DATA ENGINEERING
Переглядів 1399 місяців тому
LEARN APACHE AIRFLOW IN 11 MINUTES | DATA ENGINEERING
PYTHON THREADS AND PROCESSES | PYTHON FULL COURSE
Переглядів 269 місяців тому
PYTHON THREADS AND PROCESSES | PYTHON FULL COURSE
AGGREGATIONS IN SPARK | SPARK INTERVIEW QUESTIONS
Переглядів 369 місяців тому
AGGREGATIONS IN SPARK | SPARK INTERVIEW QUESTIONS
PYTHON IDENTITY AND EQUALITY | PYTHON FULL COURSE FREE
Переглядів 289 місяців тому
PYTHON IDENTITY AND EQUALITY | PYTHON FULL COURSE FREE
PYTHON DECORATORS IN 7 MINUTES | PYTHON FULL COURSE FREE
Переглядів 129 місяців тому
PYTHON DECORATORS IN 7 MINUTES | PYTHON FULL COURSE FREE
SPARK DATAFRAME MANIPULATIONS | SPARK INTERVIEW QUESTIONS
Переглядів 309 місяців тому
SPARK DATAFRAME MANIPULATIONS | SPARK INTERVIEW QUESTIONS
SPARK TRANSFORMATION | SPARK INTERVIEW QUESTIONS
Переглядів 499 місяців тому
SPARK TRANSFORMATION | SPARK INTERVIEW QUESTIONS

КОМЕНТАРІ

  • @node-dev-nt7et
    @node-dev-nt7et 5 днів тому

    Wow, Awesome, Thanks for the tutorial

  • @DigambarAatkar
    @DigambarAatkar 19 днів тому

    nice explanation, easy to understand .keep it up.

  • @BidyasagarPradhan-t9m
    @BidyasagarPradhan-t9m 26 днів тому

    you could have shown the execution plan of the code for better understanding.

  • @ramindersingh2961
    @ramindersingh2961 Місяць тому

    nice

  • @RaghavendraRao-hx6js
    @RaghavendraRao-hx6js 2 місяці тому

    Can i get complete Python Videos

    • @TechWithMachines
      @TechWithMachines 2 місяці тому

      Sure.. there is a playlist on python.. I will try to add more Videos on the same. Do let me know if you have any specific topics you want me to cover.

  • @IoannLenz
    @IoannLenz 4 місяці тому

    Спасибо большое за туториал.

  • @adityanjsg99
    @adityanjsg99 4 місяці тому

    Was cracking my head for some time, now I see db init is to be done. thanks

    • @TechWithMachines
      @TechWithMachines 2 місяці тому

      Great.. Hope you were able to succeed further with airflow.. Happy Learning :)

  • @AndyCreed0x
    @AndyCreed0x 5 місяців тому

    very simple and useful!

  • @ashurathi9286
    @ashurathi9286 6 місяців тому

    Maam voice is very low otherwise very good content

    • @TechWithMachines
      @TechWithMachines 6 місяців тому

      Thank you for your suggestions.. I would keep this into consideration for my next video.

  • @GAURAVSHEKHAWAT
    @GAURAVSHEKHAWAT 7 місяців тому

    simple and understable

  • @abc_cba
    @abc_cba 8 місяців тому

    very nicely explained. i wished if you help with projects using such modules/topics

    • @TechWithMachines
      @TechWithMachines 8 місяців тому

      Thank you.. !! Surely will try to come up with a tutorial project.

  • @vijiinfo
    @vijiinfo 9 місяців тому

    Nice explanation ❤. Keep doing I support you ❤

  • @zoltantorok1189
    @zoltantorok1189 11 місяців тому

    Oh, first 10 subs? And frequent uploads? Nice. Keep it up. I wonder how advanced you can make this course.

    • @TechWithMachines
      @TechWithMachines 11 місяців тому

      Thanks for watching Tech With Machines .Share this video with your friends to help the channel