SlideShare a Scribd company logo
Remove Duplicates
from Sorted Array
Eng Teong Cheah
Questions
Given an integer array nums sorted in non-
decreasing order, remove the duplicates in-place
such that each unique element appears
only once. The relative order of the elements
should be kept the same. Then return the number
of unique elements in nums
Questions
Consider the number of unique elements of nums to
be k, to get accepted, you need to do the following
things:
- Change the array nums such that the first k elements
of nums contain the unique elements in the order they
were present in nums initially. The remaining elements
of nums are not important as well as the size of nums.
- Return k.
Demo
Solution
Example Walkthrough
Let's walk through an example with
the input array nums = [1, 1, 2, 2, 3,
4, 4].
Initial state:
• nums = [1, 1, 2, 2, 3, 4, 4]
• i = 0
• j starts at 1
Example Walkthrough
First iteration (j = 1):
• nums[1] (1) is equal to nums[0] (1)
• No changes, move to the next
iteration.
Example Walkthrough
Second iteration (j = 2):
• nums[2] (2) is different from
nums[0] (1)
• Increment i to 1
• Assign nums[2] (2) to nums[1]
• nums = [1, 2, 2, 2, 3, 4, 4]
Example Walkthrough
Third iteration (j = 3):
• nums[3] (2) is equal to nums[1] (2)
• No changes, move to the next
iteration.
Example Walkthrough
Fourth iteration (j = 4):
• nums[4] (3) is different from
nums[1] (2)
• Increment i to 2
• Assign nums[4] (3) to nums[2]
• nums = [1, 2, 3, 2, 3, 4, 4]
Example Walkthrough
Fifth iteration (j = 5):
• nums[5] (4) is different from
nums[2] (3)
• Increment i to 3
• Assign nums[5] (4) to nums[3]
• nums = [1, 2, 3, 4, 3, 4, 4]
Example Walkthrough
Sixth iteration (j = 6):
• nums[6] (4) is equal to nums[3] (4)
• No changes, end of the loop.
Example Walkthrough
The final modified array is nums = [1,
2, 3, 4, 3, 4, 4] and the new length is
i + 1 = 4.
The elements beyond the new length
(i.e., 3, 4, 4) are irrelevant as the
caller of the function is expected to
consider only the first i + 1 elements
as valid.
CREDITS: This presentation template was created by Slidesgo, and includes
icons by Flaticon, and infographics & images by Freepik
Thanks!
Do you have any questions?
Please keep this slide for attribution
Resources
Leetcode Q26 Remove Duplicates from Sorted Array

More Related Content

PDF
Master the arrays and algorithms using Algotutor
VivekanandaGN1
 
PDF
Describe an algorithm that takes as input a list of n integers in no.pdf
alshaikhkhanzariarts
 
PDF
Sienna 4 divideandconquer
chidabdu
 
PDF
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
PDF
15_remove_duplicates.pdf
Emertxe Information Technologies Pvt Ltd
 
PDF
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
PDF
1-D array
Swarup Kumar Boro
 
PPTX
Merge sort
Marci McClenon
 
Master the arrays and algorithms using Algotutor
VivekanandaGN1
 
Describe an algorithm that takes as input a list of n integers in no.pdf
alshaikhkhanzariarts
 
Sienna 4 divideandconquer
chidabdu
 
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
Merge sort
Marci McClenon
 

Similar to Efficiently Removing Duplicates from a Sorted Array (20)

PDF
Demystifying Software Interviews
Michael Viveros
 
PDF
Working of Merge Sort Code
Muhammad Abdullah
 
PPTX
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
PDF
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
DOCX
Please Please Please Read the instructions and do everything li.docx
tienmixon
 
PPT
lecture 8
sajinsc
 
PDF
Most Asked Coding Questions .pdf
krishna415649
 
PDF
Skiena algorithm 2007 lecture08 quicksort
zukun
 
PPTX
07. Arrays
Intro C# Book
 
PPT
Divide and conquer
Vikas Sharma
 
PPTX
Programming data structure concept in array ppt
seetha394884
 
PPTX
Algorithm & data structures lec4&5
Abdul Khan
 
DOCX
Assignment 2 data structures National University Of Modern Languages
itsaif1312
 
PPT
Mergesort
luzenith_g
 
PDF
Quicksort analysis
Premjeet Roy
 
PDF
Become Recursion Pro in 10 days | Tutort Academy - Best Courses for Working P...
Tutort Academy
 
PPTX
Algorithms - Rocksolid Tour 2013
Gary Short
 
PPTX
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
PPTX
Data structures (Array 1 dimensional).pptx
itzsomeone50
 
PDF
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
Demystifying Software Interviews
Michael Viveros
 
Working of Merge Sort Code
Muhammad Abdullah
 
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
Skiena algorithm 2007 lecture09 linear sorting
zukun
 
Please Please Please Read the instructions and do everything li.docx
tienmixon
 
lecture 8
sajinsc
 
Most Asked Coding Questions .pdf
krishna415649
 
Skiena algorithm 2007 lecture08 quicksort
zukun
 
07. Arrays
Intro C# Book
 
Divide and conquer
Vikas Sharma
 
Programming data structure concept in array ppt
seetha394884
 
Algorithm & data structures lec4&5
Abdul Khan
 
Assignment 2 data structures National University Of Modern Languages
itsaif1312
 
Mergesort
luzenith_g
 
Quicksort analysis
Premjeet Roy
 
Become Recursion Pro in 10 days | Tutort Academy - Best Courses for Working P...
Tutort Academy
 
Algorithms - Rocksolid Tour 2013
Gary Short
 
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
Data structures (Array 1 dimensional).pptx
itzsomeone50
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
Ad

More from Eng Teong Cheah (20)

PDF
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
PDF
Monitoring Models
Eng Teong Cheah
 
PDF
Responsible Machine Learning
Eng Teong Cheah
 
PDF
Training Optimal Models
Eng Teong Cheah
 
PDF
Deploying Models
Eng Teong Cheah
 
PDF
Machine Learning Workflows
Eng Teong Cheah
 
PDF
Working with Compute
Eng Teong Cheah
 
PDF
Working with Data
Eng Teong Cheah
 
PDF
Experiments & TrainingModels
Eng Teong Cheah
 
PDF
Automated Machine Learning
Eng Teong Cheah
 
PDF
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
PDF
Hacking Containers - Container Storage
Eng Teong Cheah
 
PDF
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
PDF
Hacking Containers - Linux Containers
Eng Teong Cheah
 
PDF
Data Security - Storage Security
Eng Teong Cheah
 
PDF
Application Security- App security
Eng Teong Cheah
 
PDF
Application Security - Key Vault
Eng Teong Cheah
 
PDF
Compute Security - Container Security
Eng Teong Cheah
 
PDF
Compute Security - Host Security
Eng Teong Cheah
 
PDF
Virtual Networking Security - Network Security
Eng Teong Cheah
 
Modern Cross-Platform Apps with .NET MAUI
Eng Teong Cheah
 
Monitoring Models
Eng Teong Cheah
 
Responsible Machine Learning
Eng Teong Cheah
 
Training Optimal Models
Eng Teong Cheah
 
Deploying Models
Eng Teong Cheah
 
Machine Learning Workflows
Eng Teong Cheah
 
Working with Compute
Eng Teong Cheah
 
Working with Data
Eng Teong Cheah
 
Experiments & TrainingModels
Eng Teong Cheah
 
Automated Machine Learning
Eng Teong Cheah
 
Getting Started with Azure Machine Learning
Eng Teong Cheah
 
Hacking Containers - Container Storage
Eng Teong Cheah
 
Hacking Containers - Looking at Cgroups
Eng Teong Cheah
 
Hacking Containers - Linux Containers
Eng Teong Cheah
 
Data Security - Storage Security
Eng Teong Cheah
 
Application Security- App security
Eng Teong Cheah
 
Application Security - Key Vault
Eng Teong Cheah
 
Compute Security - Container Security
Eng Teong Cheah
 
Compute Security - Host Security
Eng Teong Cheah
 
Virtual Networking Security - Network Security
Eng Teong Cheah
 
Ad

Recently uploaded (20)

PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PPTX
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
IoT Sensor Integration 2025 Powering Smart Tech and Industrial Automation.pptx
Rejig Digital
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
This slide provides an overview Technology
mineshkharadi333
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Doc9.....................................
SofiaCollazos
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Architecture of the Future (09152021)
EdwardMeyman
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 

Efficiently Removing Duplicates from a Sorted Array

  • 1. Remove Duplicates from Sorted Array Eng Teong Cheah
  • 2. Questions Given an integer array nums sorted in non- decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums
  • 3. Questions Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things: - Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. - Return k.
  • 6. Example Walkthrough Let's walk through an example with the input array nums = [1, 1, 2, 2, 3, 4, 4]. Initial state: • nums = [1, 1, 2, 2, 3, 4, 4] • i = 0 • j starts at 1
  • 7. Example Walkthrough First iteration (j = 1): • nums[1] (1) is equal to nums[0] (1) • No changes, move to the next iteration.
  • 8. Example Walkthrough Second iteration (j = 2): • nums[2] (2) is different from nums[0] (1) • Increment i to 1 • Assign nums[2] (2) to nums[1] • nums = [1, 2, 2, 2, 3, 4, 4]
  • 9. Example Walkthrough Third iteration (j = 3): • nums[3] (2) is equal to nums[1] (2) • No changes, move to the next iteration.
  • 10. Example Walkthrough Fourth iteration (j = 4): • nums[4] (3) is different from nums[1] (2) • Increment i to 2 • Assign nums[4] (3) to nums[2] • nums = [1, 2, 3, 2, 3, 4, 4]
  • 11. Example Walkthrough Fifth iteration (j = 5): • nums[5] (4) is different from nums[2] (3) • Increment i to 3 • Assign nums[5] (4) to nums[3] • nums = [1, 2, 3, 4, 3, 4, 4]
  • 12. Example Walkthrough Sixth iteration (j = 6): • nums[6] (4) is equal to nums[3] (4) • No changes, end of the loop.
  • 13. Example Walkthrough The final modified array is nums = [1, 2, 3, 4, 3, 4, 4] and the new length is i + 1 = 4. The elements beyond the new length (i.e., 3, 4, 4) are irrelevant as the caller of the function is expected to consider only the first i + 1 elements as valid.
  • 14. CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon, and infographics & images by Freepik Thanks! Do you have any questions? Please keep this slide for attribution
  • 15. Resources Leetcode Q26 Remove Duplicates from Sorted Array