DEV Community

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

Posted on

14 1

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)

Agent.ai Challenge image

Congrats to the Agent.ai Challenge Winners 🏆

The wait is over! We are excited to announce the winners of the Agent.ai Challenge.

From meal planners to fundraising automators to comprehensive stock analysts, our team of judges hung out with a lot of agents and had a lot to deliberate over. There were so many creative and innovative submissions, it is always so difficult to select our winners.

Read more →

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay