Coders Packet

Checking duplicate elements in an user define array in Python

By Rupam Biswas

  • Duplicate items in an array.py
  • This Python program will check if there is any duplicate element available in the given array, which will be given by the user.

    At first, it will take the total number of array elements that will be present in the array. The name of this variable is 'length' and the name of the array is 'userArray' in the program.

    Then, it will take the array of elements from the user and will the array after that.

    Then, it will find the duplicate item(s) presents in the given array. If it finds any duplicate item(s) present in the given array, it will print that otherwise, it will print "There is no duplicate element in the given array"

    Here is the code:

    #Taking an array from the user
    userArray = list(map(str,input("Enter the elements:").split()))
    flag = False

    #Checking if there is any duplicate elements in the given array
    for i in range(0, len(userArray)):    
        for j in range(i+1, len(userArray)):    
            if(userArray[i] == userArray[j]):    
                #Printing the duplicate element
                print("Duplicate element is: " + userArray[j])
                flag = True

    #if there is no duplicate element found
    if(flag == False):
        print("There is no duplicate item(s) in the given array")

     

     

    Download Complete Code

    Comments

    Comments for the packet "Checking duplicate elements in an user define array in Python";
    No comments yet