python打印数组部分元素_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值