In this simple python program, we need to check for duplicate elements of an array. It's a list-based python program.
To understand this example, you should have knowledge of the following Python programming topics:
In this beginner python program, we need to print the duplicate elements in an array, if any. So we have to compare each element in the array with all other elements to know if any duplicate is there for that element. We have to continue that for every element in the array. if we found a duplicate for an element, print that element as duplicate using print in python.
After taking a predefined array, we have to compare the elements in the array. To compare each element with all other elements in the array, use a nested for loop in python. The outer
for loop is to take each element of the array and the inner
for loop is to compare that element with all other elements in that array using a python
if condition. If we found the duplicate, then print that element as duplicate and break the inner
STEP 1: Initialize an array; as we stated in the above python program, we can accept the array elements from the user too using the input method.
STEP 2: Using a print statement, print " the duplicate elements are."
STEP 3: Use an outer
for loop from 0 to the length of the array to compare each element with other elements in the array.
STEP 4: Use the inner
for loop from the next element of the outer loop to the array's length and use an if condition to compare that element with all other elements in the array.
STEP 5: print that element if any duplicate is found else break the inner for loop using print statement in the python programming language.
arr = [1, 2, 3, 4, 2, 7, 8, 8, 3]; print("The duplicate elements are: "); for i in range(0, len(arr)): for j in range(i+1, len(arr)): if(arr[i] == arr[j]): print(arr[j]);
the duplicate elements are 2 3 8