

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Program to remove duplicate entries in a linked list in Python
- Python program to remove duplicate elements from a Circular Linked List
- C# program to remove duplicate elements from a List
- How to remove duplicate entries by two keys in MongoDB?
- Python program to remove duplicate elements from a Doubly Linked List\n
- Remove duplicate tuples from list of tuples in Python
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- Program to remove duplicate characters from a given string in Python
- Avoid duplicate entries in MongoDB?
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- How to remove duplicate values inside a list in MongoDB?
- Python prorgam to remove duplicate elements index from other list
- Python program to remove rows with duplicate element in Matrix
- Write a program in Python to remove first duplicate rows in a given dataframe
- Program to find duplicate item from a list of elements in Python
Advertisements