
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to print all distinct elements of a given integer array.
Given an integer array. The elements of the array may be duplicate.Our task is to display the distinct values.
Example
Input::A=[1,2,3,4,2,3,5,6] Output [1,2,3,4,5,6]
Algorithm
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.
Example Code
# 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)
Output
Enter the size of the List ::4 Enter the Element of List :: 1 2 2 4 Distinct Elements are ::> 1 2 4
- Related Articles
- Java program to print all distinct elements of a given integer array in Java
- C# program to print all distinct elements of a given integer array in C#
- Print All Distinct Elements of a given integer array in C++
- Python Program to print all distinct uncommon digits present in two given numbers
- Python Program to print all permutations of a given string
- Write a program in Python to print the power of all the elements in a given series
- Check if all array elements are distinct in Python
- Write a program in Python to print numeric index array with sorted distinct values in a given series
- Python program to print sorted number formed by merging all elements in array
- Print sorted distinct elements of array in C language
- Python program to print elements which are multiples of elements given in a list
- Python program to print the duplicate elements of an array
- C# program to find all duplicate elements in an integer array
- Python program to print all the common elements of two lists.
- Python Program for Efficient program to print all prime factors of a given number

Advertisements