In this post we will learn about basic list program to print the elements which are duplicate .
Problem
Here is example of the problem –
Input: [ 1,2,3,3,2,2,4,]
Output: [ 2,3 ]
In this example we need to find the duplicate elements, here 2 and 3 are the duplicate elements.
Solutions
Basic approach to solve this problem is to traverse through whole list and compare each element with all the elements of the list multiple times. If we see the second occurrence of the element then we will print the element and break the inner loop.
Here is detailed program to illustrate –
Program for Duplicate Element
s=[1,2,3,4,1,2,4] n=0 nl=[] # output list while n<len(s): d=0 while d<len(s): if s[n]==s[d] and n!=d : if s[n] not in nl: nl.append(s[n]) break d=d+1 n=n+1 print(nl)
Output :
[1,2,4]
To conclude this is the basic brute force method for finding the duplicate elements.
Tags : python program to find duplicate, duplicate element program, basic python program, list duplicate
Please check out other python programs –
How Does Python 2 Compare String And Int?