DEV Community

Cover image for Subarray | Subsequence | Subset? - The difference.
Raksha Kannusami
Raksha Kannusami

Posted on

Subarray | Subsequence | Subset? - The difference.

Have you come across these questions while practising to code?

❓ Given an array of integers that might contain duplicates, return all possible subsets.
Try the Question here ⬅️

❓ Given two sequences, find the length of the longest subsequence present in both of them. Both the strings are of uppercase.
Try the Question here ⬅️

❓ Find out the maximum sub-array of non-negative numbers from an array.
Try the Question here ⬅️

If you want to attempt these questions, you should know the difference between Subset, Subsequence, and Subarray.

Let us understand these 3 terms using a simple example.

Suppose we are given an array of numbers, {10,20,30,40,50}, we need to find: Subset, subsequence, and subarray.

Subarray:

A subarray is a contiguous sequence in an array.

{10,20,30} ✅
{10,20} ✅
{10,30} ❌

Subsequence:

A subsequence is a set of elements not necessarily contiguous but should maintain order.

{10,20,30} ✅
{10,30} ✅
{10,40,30} ❌

Subset:

A subset is a subsequence, except it also includes an empty set {}.

{10,20,30} ✅
{10,40} ✅
{} ✅

FACT: All subsets are subsequences, and all subsequences are subsets!

Now you'll never confuse these terms!

Keep learning! Keep growing! 💖

Top comments (0)