用於列印給定整數陣列的所有不同元素的 Python 程式。
給定一個整數陣列。陣列元素可能是重複的。我們的任務是顯示不同值。
示例
Input::A=[1,2,3,4,2,3,5,6] Output [1,2,3,4,5,6]
演算法
Step 1: input Array element. Step 2: Then pick all the elements one by one. Step 3: then check if the picked element is already displayed or not. Step 4: use one flag variable which initialized by 0.if the element is displayed earlier flag variable is 1 and if the element is not displayed earlier flag variable is 0. Step 5: Display distinct elements.
示例程式碼
# Python program to print all distinct
# elements in a given array
def distinctelement(A, n1):
print("Distinct Elements are ::>")
for i in range(0, n1):
c = 0
for j in range(0, i):
if (A[i] == A[j]):
c = 1
break
if (c == 0):
print(A[i])
# Driver code
A=list()
n1=int(input("Enter the size of the List ::"))
print("Enter the Element of List ::")
for i in range(int(n1)):
k=int(input(""))
A.append(k)
distinctelement(A, n1)
輸出
Enter the size of the List ::4 Enter the Element of List :: 1 2 2 4 Distinct Elements are ::> 1 2 4
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP