- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to remove duplicate entries in a list in Python
Suppose we have a list of numbers called nums, we have to remove numbers that appear multiple times in the list, we also have to maintain the order of the appearance in the original list.
So, if the input is like nums = [2, 4, 6, 1, 4, 6, 9], then the output will be [2, 1, 9], as these elements have appeared only once.
To solve this, we will follow these steps −
- dict := a new map
- for each i in nums, do
- if i is not in dict, then
- dict[i] := 0
- dict[i] := dict[i] + 1
- if i is not in dict, then
- return a list with all elements e in nums where dict[e] is 1
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): dict = {} for i in nums: if i not in dict: dict[i] = 0 dict[i] = dict[i] + 1 return [k for k, v in dict.items() if v == 1] ob = Solution() nums = [2, 4, 6, 1, 4, 6, 9] print(ob.solve(nums))
Input
[2, 4, 6, 1, 4, 6, 9]
Output
[2, 1, 9]
- Related Articles
- Program to remove duplicate entries in a linked list in Python
- Python program to remove duplicate elements from a Circular Linked List
- Python program to remove duplicate elements from a Doubly Linked List\n
- How to remove duplicate entries by two keys in MongoDB?
- C# program to remove duplicate elements from a List
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- Remove duplicate tuples from list of tuples in Python
- Program to remove duplicate characters from a given string in Python
- Python program to remove rows with duplicate element in Matrix
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- How to remove duplicate values inside a list in MongoDB?
- Write a program in Python to remove first duplicate rows in a given dataframe
- Python prorgam to remove duplicate elements index from other list
- Avoid duplicate entries in MongoDB?
- Python Program to find Duplicate sets in list of sets

Advertisements