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: 3
Explanation:
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(2N)
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 elementsint 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 Codeint 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;} |
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 codepublic 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 |
Python3
# Python3 program for # the above approach# Function to find length of# the longest subsequence# consisting of distinct elementsdef 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 arrayarr = [1, 2, 3, 3, 4, 5, 5, 5]n = len(arr)# Function Callprint(longestSubseq(arr, n))# This code is contributed by divyeshrabadiya07 |
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 codepublic 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 |
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:
- Length of longest subsequence consisting of distinct adjacent elements
- 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
- Partition array into minimum number of equal length subsets consisting of a single distinct value
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest subsequence having maximum GCD between any pair of distinct elements
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- 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
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.

