Given an array **arr[]** of size **N**, the task is to find the length of the longest subsequence consisting of distinct elements only.

**Examples:**

Input:arr[] = {1, 1, 2, 2, 2, 3, 3}Output:3Explanation:

The longest subsequence with distinct elements is {1, 2, 3}Input:arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 }Output:5

**Naive Approach:** The simplest approach is to generate all the subsequences of the array and check if it consists of only distinct elements or not. Keep updating the maximum length of such subsequences obtained. Finally, print the maximum length obtained.

**Time Complexity: **O(2^{N}) **Auxiliary Space: **O(1)

**Efficient Approach: **The length of the longest subsequence containing only distinct elements will be equal to the count of distinct elements in the array. Follow the steps below to solve the problem:

- Traverse the given array keep inserting encountered elements in a Hashset.
- Since HashSet consists of only unique elements, print the size of the HashSet as the required answer after completing the traversal of the array.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find length of` `// the longest subsequence` `// consisting of distinct elements` `int` `longestSubseq(` `int` `arr[], ` `int` `n)` `{` ` ` `// Stores the distinct` ` ` `// array elements` ` ` `unordered_set<` `int` `> s;` ` ` `// Traverse the input array` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `// If current element has not` ` ` `// occurred previously` ` ` `if` `(s.find(arr[i]) == s.end()) {` ` ` `// Insert it into set` ` ` `s.insert(arr[i]);` ` ` `}` ` ` `}` ` ` `return` `s.size();` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given array` ` ` `int` `arr[] = { 1, 2, 3, 3, 4, 5, 5, 5 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `cout << longestSubseq(arr, n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to find length of ` `// the longest subsequence ` `// consisting of distinct elements ` `static` `int` `longestSubseq(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Stores the distinct ` ` ` `// array elements ` ` ` `Set<Integer> s = ` `new` `HashSet<>(); ` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `{ ` ` ` ` ` `// If current element has not ` ` ` `// occurred previously ` ` ` `if` `(!s.contains(arr[i]))` ` ` `{ ` ` ` ` ` `// Insert it into set ` ` ` `s.add(arr[i]); ` ` ` `} ` ` ` `} ` ` ` `return` `s.size(); ` `}` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` ` ` `// Given array ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `3` `, ` `4` `, ` `5` `, ` `5` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Function call ` ` ` `System.out.println(longestSubseq(arr, n)); ` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for ` `# the above approach` `# Function to find length of` `# the longest subsequence` `# consisting of distinct elements` `def` `longestSubseq(arr, n):` ` ` `# Stores the distinct` ` ` `# array elements` ` ` `s ` `=` `set` `()` ` ` `# Traverse the input array` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# If current element has not` ` ` `# occurred previously` ` ` `if` `(arr[i] ` `not` `in` `s):` ` ` `# Insert it into set` ` ` `s.add(arr[i])` ` ` `return` `len` `(s)` `# Given array` `arr ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `3` `, ` ` ` `4` `, ` `5` `, ` `5` `, ` `5` `]` `n ` `=` `len` `(arr)` `# Function Call` `print` `(longestSubseq(arr, n))` `# This code is contributed by divyeshrabadiya07` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System;` `using` `System.Collections.Generic; ` `class` `GFG{` ` ` `// Function to find length of ` `// the longest subsequence ` `// consisting of distinct elements ` `static` `int` `longestSubseq(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` ` ` `// Stores the distinct ` ` ` `// array elements ` ` ` `HashSet<` `int` `> s = ` `new` `HashSet<` `int` `>();` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{ ` ` ` ` ` `// If current element has not ` ` ` `// occurred previously ` ` ` `if` `(!s.Contains(arr[i]))` ` ` `{ ` ` ` ` ` `// Insert it into set ` ` ` `s.Add(arr[i]); ` ` ` `} ` ` ` `} ` ` ` `return` `s.Count; ` `}` ` ` `// Driver code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` ` ` `// Given array ` ` ` `int` `[]arr = { 1, 2, 3, 3, 4, 5, 5, 5 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Function call ` ` ` `Console.Write(longestSubseq(arr, n)); ` `}` `}` `// This code is contributed by rutvik_56` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity: **O(N) **Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Longest subsequence consisting of alternate vowels and consonants
- Length of largest subsequence consisting of a pair of alternating digits
- Length of the smallest sub-string consisting of maximum distinct characters
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Palindromic Subsequence of two distinct characters
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Length of Longest Balanced Subsequence
- Length of longest strict bitonic subsequence
- Length of longest common subsequence containing vowels
- Find length of longest Fibonacci like subsequence
- Length of Longest Prime Subsequence in an Array
- Length of longest subsequence of Fibonacci Numbers in an Array
- Find the length of the longest subsequence with first K alphabets having same frequency
- Length of longest Powerful number subsequence in an Array
- Length of Longest Perfect number Subsequence in an Array
- Length of longest increasing index dividing subsequence

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.