How do I check duplicate elements present in an array?

 In Python It is a simple program, Let see step by step.

Steps:    

  • Firstly initialize a list1 with the duplicate elements.
  • initialize an empty list2.
  • Take a for loop for list1.
  • check if the list1 element is not in list2 then append the element of list1 into list2.

     

 Let’s See an example of it:

      

  1. # Using naive method 
  2. list1=[1,2,3,1,3,4,5,6,7,3,4,6,7] 
  3. print("original list is: " +str(list1)) 
  4. list2=[] 
  5. for i in list1: 
  6. if i not in list2: 
  7. list2.append(i) 
  8. print ("list after removing duplicates element is : " + str(list2)) 
  9.  
  10. # Using list comprehension method 
  11.  
  12. list1=[1,2,3,1,3,4,5,6,7,3,4,6,7] 
  13. print("original list is: " +str(list1)) 
  14. list2=[] 
  15. [list2.append(i) for i in list1 if i not in list2] 
  16. print("list after removing duplicates element is: " + str(list2))
 

Check my Quora Id where I have already discussed this question.

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Programming shark backlink data index 67