• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove Duplicates from Sorted Array in Python

Remove Duplicates from Sorted Array in Python

March 11, 2022 Leave a Comment

To remove duplicates from a sorted array in Python without using extra space, we can define a function which will loop over your list and delete any duplicates.

def removeDuplicates(arr):
    for i in range(len(arr)-1,0,-1):
        if arr[i] == arr[i-1]:
            del arr[i]
    return arr

sorted_list = [1,2,2,3,3,4,5,5,8,8,8,8,9,9,9]

print(removeDuplicates(sorted_list))

#Output:
[1, 2, 3, 4, 5, 8, 9]

Another way to remove duplicates from a list is to convert it to a set and then back to a list.

sorted_list = [1,2,2,3,3,4,5,5,8,8,8,8,9,9,9]

print(list(set(sorted_list)))

#Output:
[1, 2, 3, 4, 5, 8, 9]

When working with collections in Python, a very common task to do is remove duplicates.

In Python, arrays are called lists, and we can easily delete all duplicate values from a sorted list with a loop which checks if the next element is the same as the current element and deletes it if they are the same.

This function removes the duplicates in place and doesn’t take any extra space.

Below is a function in Python which removes all duplicates from a sorted array.

def removeDuplicates(arr):
    for i in range(len(arr)-1,0,-1):
        if arr[i] == arr[i-1]:
            del arr[i]
    return arr

sorted_list = [1,2,2,3,3,4,5,5,8,8,8,8,9,9,9]

print(removeDuplicates(sorted_list))

#Output:
[1, 2, 3, 4, 5, 8, 9]

Removing Duplicates from List with set() in Python

A set is an unordered collection of unique elements. On the other hand, lists are ordered and can contain duplicates.

Converting a list to a set creates a new set with the same items as the list and removes all duplicates.

To remove duplicates from a list, we can use set() to convert a list to a set, and then use list() to convert it back.

This does take extra space but will sort and remove duplicates all in one step.

Below is how to remove duplicates from a list with the set() function in Python.

lst = [0,1,2,3,0,0,3]

print(list(set(lst)))

#Output:
[0,1,2,3]

Hopefully this article has been useful for you to learn how to remove duplicates from a sorted array in Python.

Other Articles You'll Also Like:

  • 1.  pandas head – Return First n Rows from DataFrame
  • 2.  Python not in – Check if Value is Not Included in Object
  • 3.  Convert String to Boolean Value in Python
  • 4.  Draw Circle in Python Using turtle circle() Function
  • 5.  Using Python to Check If List of Words in String
  • 6.  Remove Leading and Trailing Characters from String with strip() in Python
  • 7.  Get Day of Year from Date in pandas DataFrame
  • 8.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 9.  Sort Series in pandas with sort_values() Function
  • 10.  Rename Key in Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy