Write a program to check duplicate values ​​in Python

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

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

Example :

Input: [1, 2, 3, 1] Output: True

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

Write a program to check duplicate values ​​in Python

To write a duplicate value checker program 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 duplicate value checker program in Python:

def containsDuplicate(nums): for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] == nums[j]: return True else: return False nums = [1,2,3,1] print(containsDuplicate(nums))

The returned result is:

True

The function above works like this:

  • A loop is run from the first position (i) to the last position of the array.
  • Another loop is run from position j = i + 1 to the end of the array.
  • If the number at position i is equal to the number at position j, return True.
  • Otherwise, return False.

You can use this Python function for any data type.

Conclude

Checking for duplicates in an array or in any data structure is one of the common interview questions. In interviewing or testing candidates' qualifications, you need to use an algorithm to solve this problem.

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