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! 💖

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay