Flatten Arrays using C#

Ranga Dewasinghe
Mar 1, 2019 · 1 min read

Here I came up with a common interview question that you might get in an interview.

So the question goes as follows,

Write a function, that when given a multi-level array, flattens it into a 1D array.
Sample Input — [1, 2, [3, [4, 5]], 6]
Sample Output — [1, 2, 3, 4, 5, 6]

One of the possible ways that we can use to solve this is a recursive approach because we really don't know how many levels of nested arrays are going to be there.
So, I came up with the following solution using LINQ.

Please find the full solution with unit testing in Github.

Thank you & Good Luck!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store