# artemkorsakov's solution

## to Flatten Array in the C# Track

Published at May 03 2019 · 0 comments
Instructions
Test suite
Solution

Take a nested list and return a single flattened list with all values except nil/null.

The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.

For Example

input: [1,[2,3,null,4],[null],5]

output: [1,2,3,4,5]

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit FlattenArray.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

Interview Question https://reference.wolfram.com/language/ref/Flatten.html

### FlattenArrayTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class FlattenArrayTest
{
[Fact]
public void No_nesting()
{
var array = new object[]
{
0,
1,
2
};
var expected = new[] { 0, 1, 2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}

[Fact(Skip = "Remove to run test")]
public void Flattens_array_with_just_integers_present()
{
var array = new object[]
{
1,
new object[] { 2, 3, 4, 5, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}

[Fact(Skip = "Remove to run test")]
public void Number_5_level_nesting()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, 100, 4, new object[] { new object[] { new object[] { 50 } } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, 4, 50, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}

[Fact(Skip = "Remove to run test")]
public void Number_6_level_nesting()
{
var array = new object[]
{
1,
new object[] { 2, new object[] { new object[] { 3 } }, new object[] { 4, new object[] { new object[] { 5 } } }, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}

[Fact(Skip = "Remove to run test")]
public void Number_6_level_nest_list_with_null_values()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, new object[] { new object[] { 100 } }, null, new object[] { new object[] { null } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}

[Fact(Skip = "Remove to run test")]
public void All_values_in_nested_list_are_null()
{
var array = new object[]
{
null,
new object[] { new object[] { new object[] { null } } },
null,
null,
new object[] { new object[] { null, null }, null },
null
};
Assert.Empty(FlattenArray.Flatten(array));
}
}``````
``````﻿using System.Collections;

public static class FlattenArray
{
public static IEnumerable Flatten(IEnumerable input)
{
var list = new ArrayList();

foreach (var obj in input)
{
if (obj == null)
{
continue;
}

if (obj is IEnumerable)
{
var inner = Flatten((IEnumerable)obj);
foreach (var objInner in inner)
{
}
}
else
{
}
}

return list;
}
}``````