Write a program to find duplicate values ​​in Python

If you want to learn how to write a program to find duplicate values ​​in Python, this is the article for you.

Topic : Find duplicate values ​​in an array or any data structure. A duplicate value is a value that occurs at least twice in an array or in a list or data structure.

Example :

 Input: ["Hoa", "Lan", "Trang", "Minh", "Hoa", "Ngoc", "Trang"] Output: ["Hoa", "Trang"]

In this article, TipsMake.com will learn with you how to write a program to determine duplicate values ​​in the Python programming language.

Write a program to find duplicate values ​​in Python

To write a program to find duplicates in Python, we need to declare a Python function that can take a list of values ​​of any data type. Here is the sample code of the program to find duplicate values ​​in Python:

def find_duplicates(x): length = len(x) duplicates = [] for i in range(length): n = i + 1 for a in range(n, length): if x[i] == x[a] and x[i] not in duplicates: duplicates.append(x[i]) return duplicates names = ["Hoa", "Lan", "Trang", "Minh", "Hoa", "Ngoc", "Trang"] print(find_duplicates(names))

The returned result is:

['Hoa', 'Trang']

The function above works like this:

  • The above function takes a list as input.
  • It then calculates the length of the list.
  • Next, it searches for the same value in the list found on the first index.
  • If it finds multiple values, it will add that value to another list containing duplicate values.
  • This process is continued until the loop reaches the last index of the list.
  • At the end of the process, it returns a list of duplicate values.

You can use this Python function for any data type.

Conclude

Finding duplicates in an array or in any data structure is one of the common interview questions. Python provides many built-in functions for finding duplicate values, but in interviewing or hiring candidate qualifications you need to use an algorithm instead of a built-in function.

TipsMake.com hopes that this article will be useful to you.

Related posts
Other Python articles
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup