[Question]
  [
**This question already has answers here**:
 
 
[Make a list flat](/questions/248245/make-a-list-flat)
 (22 answers)
 
Closed 1 year ago.
In this challenge, your task is to create a program which takes in a nested array and returns a single-dimensional flattened array. For Example `[10,20,[30,[40]],50]` should output `[10,20,30,40,50]`.
---
**Input**
The input will be a nested array (eg. `[10,20,[[[10]]]]`). It will contain only Integers (both negative and positive), Strings and Arrays. You can take the input as function argument, STDIN or whatever suits your language. You can assume that the input array won't have an empty array.
---
**Output**
The output will be a flatted single-dimensional array with the same elements of same type as in the nested array and in the SAME order. 
---
**Test Cases**
```
[10,20,30] -> [10,20,30]
[[10]] -> [10]
[["Hi"],[[10]]] -> ["Hi",10]
[[[20],["Hi"],"Hi",20]] -> [20,"Hi","Hi",20]
[[["[]"],"[]"]] -> ["[]","[]"]
```
---
Feel free to ask for any clarification by using comments. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins!
**Note:** *If your language contains a built-in for this, then you must NOT use it.*
---
**Edit**
Please also include a link to a website where your code can be executed.
  
[Answer]
# K, 3 bytes
```
,//
```
This is a fairly common idiom. "Join over converge".
[try it here with oK](http://johnearnest.github.io/ok/index.html?run=%20%2C%2F%2F%281%3B%282%3B3%3B%284%3B5%29%29%29).
How it works:
Join (`,`) fuses together atoms or lists to produce a list. Over (`/`) takes a verb (in this case join) and applies it between each element of a list, left to right. Thus, the compound `,/` will flatten all the top level elements of the list. The symbol `/` actually has different meanings depending on the valence (number of arguments) of the verb with which it is compounded. When we provide `,/` as the verb, the final `/` acts as "converge"- it repeatedly applies `,/` to the input until it stops changing. Some other languages call a feature like this a "fixed point combinator". By repeatedly fusing bottom level lists, you will eventually arrive at a single flat list, and none of the operations will perturb the order of elements. This seems to solve the problem.
[Answer]
# JavaScript (ES6), 35 bytes
Inspired by [@user81655's answer](https://codegolf.stackexchange.com/a/80102/7588):
```
f=a=>a.map?[].concat(...a.map(f)):a
```
[Answer]
## Mathematica, ~~16~~ 14 bytes
```
{##&@@#&//@#}&
```
An unnamed function which takes and returns a list, e.g.:
```
{##&@@#&//@#}& @ {{{20}, {"Hi"}, "Hi", 20}}
(* {20, "Hi", "Hi", 20} *)
```
### Explanation
Syntactic sugar party!
To understand how this works, note that every expression in Mathematica is either an atom (e.g. numbers, strings, symbols) or a compound expression of the form `f[a, b, c, ...]`, where `f`, `a`, `b`, `c` are themselves arbitrary expressions. Here, `f` is called the *head* of the expression. Everything else on top of that is just syntactic sugar. E.g. `{a, b, c}` is just `List[a, b, c]`.
We start with `//@` which maps a functions over *all* levels of a list. For instance:
```
f //@ {{{20}, {"Hi"}, "Hi", 20}}
(* f[{f[{f[{f[20]}], f[{f["Hi"]}], f["Hi"], f[20]}]}] *)
```
Note that this maps `f` over atoms as well as compound expressions. What we're now looking for is a way to get rid of the list heads and keep everything else.
The `Apply` function is normally used to feed the elements of a list as separate arguments to a function, but its actual definition is more general and simply replaces the head of an expression. E.g. `Apply[g, f[a, b]]` gives `g[a, b]`.
Now there's a special "head" called `Sequence` that simply vanishes. E.g. `{a, Sequence[b, c], d}` just evaluates to `{a, b, c, d}`. The idea for flattening the list is to replace the heads of all inner lists with `Sequence` so that they get splatted into their surrounding list. So what we want is to `Apply` the head `Sequence` to the lists. Conveniently if we `Apply` something to an atom, it just leaves the atom unchanged, so we don't have to distinguish between types of expressions at all.
Finally, there's one small issue: `f` is also applied to the outermost level, so that it also removes the outermost `List`, which we don't want. The shortest way to counter that is simply to wrap the result in a list again, such that the surrounding `Sequence` can safely vanish.
Note that there's neither `Apply` nor `Sequence` in the code. `@@` is an operator form of `Apply` and `##&` is a standard golfing trick to shorten the long built-in name `Sequence`. So ungolfing everything a bit, we get something like:
```
flatten[list_] := { MapAll[Apply[Sequence], list] }
```
For more details on how and why the `##&` works, see the section on "Sequences of arguments" [in my answer for the Mathematica tips](https://codegolf.stackexchange.com/a/45188/8478).
[Answer]
## Python 2, 43 bytes
```
f=lambda l:[l]*(l*0!=[])or sum(map(f,l),[])
```
On a list, recurses on the elements and concatenates the results. On a string or number, encases in a singleton list.
Unfortunately, Python 2's ordering for types `int < list < string` sandwiches `list` between the others, requiring two inequalities to check. So, instead, `l*0` is checked against the empty list `[]`, otherwise giving `0` or `""`.
[Answer]
# Ruby, ~~43~~ ~~42~~ 34 bytes
Recursive solution. Now with exception handling! (might as well credit @akostadinov for inspiring the change though)
```
f=->a{a.map(&f).inject:+rescue[a]}
```
[IDEOne link](http://ideone.com/Om3WAT)
[Answer]
# JavaScript (ES6), 41 bytes
```
f=a=>[].concat(...a.map(v=>v.pop?f(v):v))
```
```
<textarea id="input" rows="6" cols="40">[[[20],["Hi"],"Hi",20]]</textarea><br /><button onclick="result.textContent=JSON.stringify(f(eval(input.value)))">Go</button><pre id="result"></pre>
```
[Answer]
# [Perl 6](http://perl6.org), 24 bytes
```
{gather {$_».&{.take}}}
```
### Explanation:
```
{ # has $_ as an implicit parameter
  gather {
    $_\ # the parameter from the outer block
    »\  # for each single value in the structure
    .&( # call the following block as if it was a method
      { # this block has its own $_ for a parameter
        .take # call the .take method implicitly on $_
      }
    )
  }
}
```
### Test:
```
#! /usr/bin/env perl6
use v6.c;
use Test;
my &flatten = {gather {$_».&{.take}}}
my @tests = (
  [10,20,30], [10,20,30],
  [[10,],], [10,],
  [["Hi",],[[10,],],], ["Hi",10],
  [[["[]",],"[]"],], ["[]","[]"],
);
plan @tests / 2;
for @tests -> $input, $expected {
  # is-deeply cares about the exact type of its inputs
  # so we have to coerce the Seq into an Array
  is-deeply flatten($input).Array, $expected, $input.perl;
}
```
```
1..4
ok 1 - $[10, 20, 30]
ok 2 - $[[10],]
ok 3 - $[["Hi"], [[10],]]
ok 4 - $[[["[]"], "[]"],]
```
[Answer]
## Haskell, 43 bytes
```
data D a=L a|N[D a]
f(L x)=[x]
f(N l)=f=<<l
```
Haskell has neither nested lists with different depths of the sublists nor mixed types for the list elements. For nesting I define a custom data type `D` which is either a leaf `L` that holds some element or a node `N` which is a list of `D`s. For the mixed elements I use the predefined data type `Either` which combines two types into one, here `Either String Integer`. The new type `D` and the flatten function `f` are fully polymorphic in the type of the leaf elements, so I don't have to take extra care of anything regarding `Either`. 
Usage example: `f (N[N[L(Right 20)], N[L(Left "Hi")], L(Left "Hi") , L(Right 20)])` -> `[Right 20,Left "Hi",Left "Hi",Right 20]`.
[Answer]
# Pyth, ~~7~~ ~~6~~ 5 bytes
```
us+]Y
```
Try it online: [Demonstration](http://pyth.herokuapp.com/?code=us%2B]Y&input=[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]&test_suite_input=[10%2C20%2C30]%0A[[10]]%0A[[%22Hi%22]%2C[[10]]]%0A[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]%0A[[[%22[]%22]%2C%22[]%22]]&debug=0) or [Test Suite](http://pyth.herokuapp.com/?code=us%2B]Y&input=[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]&test_suite=1&test_suite_input=[10%2C20%2C30]%0A[[10]]%0A[[%22Hi%22]%2C[[10]]]%0A[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]%0A[[[%22[]%22]%2C%22[]%22]]&debug=0)
But of course, there is also a build-in function, that handles the task in just 2 bytes: `.n` ([Test Suite](http://pyth.herokuapp.com/?code=.n&input=[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]&test_suite=1&test_suite_input=[10%2C20%2C30]%0A[[10]]%0A[[%22Hi%22]%2C[[10]]]%0A[[[20]%2C[%22Hi%22]%2C%22Hi%22%2C20]]%0A[[[%22[]%22]%2C%22[]%22]]&debug=0))
[Answer]
## JavaScript (Firefox 30-57), 43 bytes
```
f=a=>a.map?[for(b of a)for(c of f(b))c]:[a]
```
Just because I could even avoid using `concat`.
[Answer]
## Julia, 29 bytes
```
f(x,y=vcat(x...))=x==y?x:f(y)
```
This is recursive splatting into a concatenate function until a reaching a fix point. Example 
```
julia> f([1,[2,[3,[4,[5,[6]]]]]])
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
```
[Answer]
# Perl, ~~34~~ 29 bytes
Functions.
If needs to flatten to list like `my @a = f(@a)`, 29 bytes:
```
sub f{map{ref()?f(@$_):$_}@_}
```
[Test it on Ideone](http://ideone.com/hlPjyf)
If needs to flatten to array ref like `my $a = f($a)`, 34 bytes:
```
sub f{[map{ref()?@{f(@$_)}:$_}@_]}
```
[Test it on Ideone](http://ideone.com/V8Xg0Q).
# Perl 5.22.0+, 27 bytes
Thanks to [hobbs](/u/1683).
If needs to flatten to list like `my @a = f(@a)`, 27 bytes:
```
sub f{map{ref?f(@$_):$_}@_}
```
[Test it on JDoodle](https://jdoodle.com/a/Rp)
If needs to flatten to array ref like `my $a = f($a)`, 32 bytes:
```
sub f{[map{ref?@{f(@$_)}:$_}@_]}
```
[Test it on JDoodle](https://www.jdoodle.com/a/Ro).
[Answer]
# [Attache](https://github.com/ConorOBrien-Foxx/Attache), 14 bytes
```
{Reap[Sow@>_]}
```
[Try it online!](https://tio.run/##bY4xC8IwEIX3/IpQHFQixFYXwSK4OImo23FIqal20WICDuJvj3eNKUUMNNx97@X1Fc4V5dX4Si6W/rU3RQOH@3OVn/Dth2MJU61SrTKNcpL3NgE0Y4S8Jps6QRVw4ExUECHVpAVLi9P4mOJaECmbE0D28f1NojHsYjwSYmvM2cKgacoLit2jvrmjsW5dWGOhUhKEpMNdJaVLaqsCabvF@W@lTuxXUF0exUFG34ytc/2jAP@JJFYEov8A "Attache – Try It Online")
Fortunately, Attache has a "vectorization" operator, which applies a function at the atoms of a list. In this case, all we need to do is to set up a reaper with `Reap` and `Sow` all atoms of the input `_` with `@>`. I think it's quite elegant.
## Alternatives
**15 bytes:** `Fixpoint{`'^^_}`
**16 bytes:** `Fixpoint!&Concat`
**17 bytes:** `{q:=[]q&Push@>_q}`
**17 bytes:** `Fixpoint[&Concat]`
[Answer]
## [Retina](https://github.com/mbuettner/retina/), 30 bytes
```
1>`("(\\.|[^"])+")|[][]
$1
$
]
```
[Try it online!](http://retina.tryitonline.net/#code=JShHYAoxPmAoIihcXC58W14iXSkrIil8W11bXQokMQokCl0&input=WzEwLDIwLDMwXQpbWzEwXV0KW1siSGkiXSxbWzEwXV1dCltbWzIwXSxbIkhpIl0sIkhpIiwyMF1dCltbWzIwXSxbIkhbXVwiW11pIl0sIkhcXFwiW11pIiwyMF1d) (The first line is only used to run multiple test cases at once.)
Retina has no concept of arrays, string literals or numbers, so I decided to go with a "common" input format of `[...,...]` style arrays and `"`-delimited strings, where `\` can be used inside the strings to escape any character (in particular `"` and `\` itself).
The program itself simply matches either a full string or a square bracket, and replaces them with `$1` which keeps strings and removes square brackets. The limit `1>` skips the first match so that we don't remove the leading `[`. However, this does remove the trailing `]`, so we add it back in in a separate stage.
[Answer]
## Pyke, 11 bytes
```
.F~]+=])K~]
```
[Try it here!](http://pyke.catbus.co.uk/?code=.F~%5D%2B%3D%5D%29K~%5D&input=%5B%5B%5B20%5D%2C%5B%22Hi%22%5D%2C%22Hi%22%2C20%5D%5D)
Explanation:
```
.F~]+=])    - Deep for loop
  ~]        -    contents of `]` ([] by default)
    +       -  ^+i
     =]     - `]` = ^
        K~] - Output value
        K   - Remove the output from the for loop
         ~] - Return the contents of `]`
```
### Or 7 bytes after a bugfix
```
M?+]K~]
```
[Try it here!](http://pyke.catbus.co.uk/?code=M%3F%2B%5DK~%5D&input=%5B%5B%5B20%5D%2C%5B%22Hi%22%5D%2C%22Hi%22%2C20%5D%5D)
Explanation:
```
M?+]    - Deep map
 ?+]    -  `]` = `]`+i
    K~] - Output value
    K   - Remove the output from the for loop
     ~] - Return the contents of `]`
```
### Or even 2 bytes if printing to stdout is allowed (This might come under built-ins)
```
M
<newline required>
```
[Try it here!](http://pyke.catbus.co.uk/?code=M%0A&input=%5B%5B%5B20%5D%2C%5B%22Hi%22%5D%2C%22Hi%22%2C20%5D%5D)
This deeply applies the `print_newline` function to every non-sequence item in the input and recurses for sequence items.
[Answer]
# Java (v8) 390 276 bytes
```
public static Object[] f(final Object[]a) {
    List<Object>r=new ArrayList<>();boolean t=false;int n=0;
    for(final Object p:a)
        if(t=p instanceof Object[]){for(final Object q:(Object[])p) r.add(q);}
        else r.add(p);
    return(t)?f(r.toArray()):r.toArray();
}  
```
Just for completeness and all that. :) Can't say Java's code-efficient.
[Answer]
## Python, 57 bytes
```
f=lambda a:sum([list==type(x)and f(x)or[x]for x in a],[])
```
Try it online: [Python 2](http://ideone.com/fork/vHoqK3), [Python 3](http://ideone.com/fork/cFnU0B)
Thanks to Kevin Lau for the `list==type(x)` trick.
[Answer]
# Ruby
there is builtin `flatten` method.
You can run here:
<http://www.tutorialspoint.com/execute_ruby_online.php>
One 43 bytes, but thought to share:
```
f=->a{a.inject([]){|r,e|r+(f[e]rescue[e])}}
```
One 45 bytes that is more efficient than the previous and the other ruby answer:
```
f=->a{a.map{|e|Array===e ?f[e]:[e]}.inject:+}
```
here's benchmark:
```
require 'benchmark'
n=10^9
arr=[[[20],[[[[[[[[123]]]]]]]],"ads",[[[[[[[4]]]]]]],5,[[[[[[[[[[6]]]]]]]]]],7,8,[[[[[[[[[[9]]]]]]]]]],[[[[[[[[[[0]]]]]]]]]],[[[[[[[[[[[["Hi"]]]]]]]]]]]],[[[[[["Hi"]]]]]],[[[[[20]]]]]]]
Benchmark.bm do |x|
  x.report { f=->a{a.map(&f).inject:+rescue[a]}; f[arr] }
  x.report { f=->a{a.map{|e|e!=[*e]?[e]:f[e]}.inject:+}; f[arr] }
  x.report { f=->a{a.inject([]){|r,e|r+(f[e]rescue[e])}}; f[arr] }
  x.report { f=->a{a.map{|e|Array===e ?f[e]:[e]}.inject:+}; f[arr] }
end
```
result:
```
       user     system      total        real
   0.010000   0.000000   0.010000 (  0.000432)
   0.000000   0.000000   0.000000 (  0.000303)
   0.000000   0.000000   0.000000 (  0.000486)
   0.000000   0.000000   0.000000 (  0.000228)
```
[Answer]
# Perl, ~~39~~ 34 + 1 (`-p` flag) 35 bytes
Oneliner. Inspired by [Martin Büttner](/a/80109/34575).
```
#!perl -p
s/("(\\.|[^"])+"|]$|^\[)|[][]/$1/g
```
[Test it on Ideone](http://ideone.com/sBPlDe).
[Answer]
## Clojure, 68 bytes
```
(def f #(if(some vector? %)(f(mapcat(fn[z](if(vector? z)z[z]))%))%))
```
`mapcat` first applies function to each element and then concats results. So every time it concats one 'nesting level' is lost. Concat does not work on not sequences so elements have to be wrapped into vector if they're not vector.
You can try it here: <http://www.tryclj.com>
```
(f [[[20],["Hi"],"Hi",20]])
(f [[["[]"],"[]"]])
```
[Answer]
# ANSI C, 193 bytes
```
#define b break;
#define c case
#define p putch(_);
char f;main(_){switch(_){c 1:putch(91);b c 34:f^=1;p b c 91:f&&p b c 93:f&&p b c 10:c 13:putch(93);return;default:p}_=getch();main(_);}
```
:-/, any suggestions? Btw, I did try to find an online source to compile this but the WL is strict for this code to compile. It will work for VS and gcc otherwise.
[Answer]
# JavaScript 20 bytes
```
a=>(a+[]).split(',')
```
The array + array is equal to array.toString
[Answer]
# [Haskell](https://www.haskell.org/) + [free](https://hackage.haskell.org/package/free-5.1.7/docs/Control-Monad-Free.html), 7 bytes
In order to represent a ragged list in Haskell we use a free monad of lists. Luckily for us provided with the free monad is `retract`, which does a lot of different things, but when given the particular type here it flattens the free monad of lists down to a single list.
```
retract
```
[Try it online!](https://tio.run/##ZZHBatwwEIbvfoqB5GBDbGdbSulS@9ImEEiXUnIrIQy2ZIu1JCPJdEPaZ9@OLK26S0/W/P/MfDPjEe2eTdPxrbyCR1TDggODH6j2u6fXmVm4Kv9kZQlfGRdKOKGVhU7PgvXAjZbgRgZWL6ZjoPkaccMYSN0vE721AcesE2rwTUbnZrut6xG7PWGqMbArbYZ6Dlrty8sP1ab6WPe6s7U1Xf1FK2f0VH3TCvvqnjKq0ckp69Eh@BA4YAbQwPfFsPX5O@g5h/yUUBRZJpR1qGjY@0V1jqbj0LQpiKkF/BqZYdSFS5x9Cgw6aeCDPICKxMw9ILmhD9lNHIPEz9etlyjHn/ph9/iwuwsAf@LMMGewc7DdwrpmnCzODmW77njKSvyG@pG4GHXurmVog40W2raBaNINHDMec3GCPEKoZXmJxVDwcsY8afMoIkt6dQ2T4feVdHKup37NSZvJANQGpwkOUBHp4EkSDv/RJbFSh5eLu6e1k8/PN/cdwub//CNv4hmOEoWipF7ThWYjlIPrVP9zpWxuwq99F7/vb@DMPamf4ndz@/xcHP8C "Haskell – Try It Online")
But we can also make our own solution using other tools, like `foldFree`:
# 11 bytes
```
foldFree id
```
[Try it online!](https://tio.run/##ZZHBatwwEIbvfoqB5GBDbGdbSulS@9ImEEiXUnIrIQy2ZIu1JCPJdEPaZ9@OLK26S0/S/P9ovpnRiHbPpun4Vl7BI6phwYHBD1T73dPrzCxclX@ysoSvjAslnNDKQqdnwXrgRktwIwOrF9Mx0HyNuGEMpO6Xie7agGPWCTX4IqNzs93W9YjdnjDVGNiVNkM9B632z8sP1ab6WPe6s7U1Xf1FK2f0VH3TCvvqnjKq0ckp69Eh@BA4YAbQwPfFsPX6O@g5h/yUUBRZJpR1qKjZ@0V1jrrj0LQpiKkF/BqZYVSFS5x9Cgw6aeCDPICKxMw9ILmhDtlNbIPEz9etlyjHr/ph9/iwuwsAv@LMMGewc7Ddwjpm7Cz2DmW7znjKSvyG6pG4GHXurs/QBhsttG0D0aQdOGY85mIFeYRQyfISi@HByxnzpM2jiCzp1TVMhp9X0sq5nvo1J00mA1AbnCY4QEWkgydJOPxHl8RKFV4u9p7GTj4/n9xXCJP/84@8SYHojxKFosRe05ZmI5SD61Tj50ra3ITvfRfP9zdw5p7UT/Hc3D4/F8e/ "Haskell – Try It Online")
However `foldFree` basically is just a more general version of `retract`, and we can do it without it:
# 21 bytes
```
iter(>>=id).fmap(:[])
```
[Try it online!](https://tio.run/##ZZFRa9swEMff/SkOmgcbanlZGWNh9su2QKELZewtlCJsyRaxJCPJLKXrZ09PlqIl7Mm6/939f3fngdoDG8fTa3kDD1T1M@0Z/KLqsPv9MjELN@VbVpbwnXGhhBNaWWj1JFgH3GgJbmBg9WxaBpovETeMgdTdPOJbG3DMOqF6bzI4N9lNVQ20PSCGDIFNtOmrKWiVby8/kTX5XHW6tZU1bfVNK2f0SH5qRTuyxQoyODlmHXUUfAgcaAZQw@Ns2PL8G/ScQ34uKIosE8o6qnDY7axah9NxqJsUxNIC/gzMMHThkk6@BHqdNPBBHkBFYuYekLLBB9N1HAPFr6vGS1jjT32/e7jf/QgAf@LMMGdo62CzgWXNOFmcHcpm2fFclfg1@qE4G3WZXdqoDWlqoWlqiEm8gWPGY65OkEcIWpbXWBoani@YZ20aRGRJry5hSvh9JZ6c67FbatJmMgC1oeMIRyBIOnqShON/dIms5PB8dfe0dsrzy829Q9j8X/7Eaz9fjqroCuKvn2/2T8VJUqGwpdN4r8kI5WCV3PYLc30bfvTH@L27hYvsWf0Sv@sPT@j6Dg "Haskell – Try It Online")
If we want to use *none* of the functions provided by free just the definition of the monad and prelude functions we can do:
# 29 bytes
```
f(Pure a)=[a]
f(Free a)=a>>=f
```
[Try it online!](https://tio.run/##ZVHBatwwEL37KwaSgw2xnW0ppUvsS5tAIF1K6S0sYbAl26wlGUmmG9J@@3ZkyeouOVnz3pv3ZsY9mgMbx9NbfgVPKLsZOwY/UR52v14nZuAq/5vkOXxjfJCDHZQ00KhpYC1wrQTYnoFRs24YKL5UXDMGQrXzSG@lwTJjB9k5k97ayWzLssfmQDFF77MLpbty8ljp2vNPxab4XLaqMaXRTflVSavVWHxXEtvigRRFb8WYtGgRXAkcMAGo4Mes2fL84/GUQ7oKsixJBmksShr2YZaNpek4VHUsgjSD3z3TjFy4wMlJoFMRA1ekPiiLmakLiKz3IboKYxB4d107iDTu1I@7p8fdvQ9wJ040sxobC9stLGuGycLskNfLjqsq5lfkR@Cs5Tm7tKHxNBqo6woCSTewTLuYixOkIYQs88tY9A0vZ5krNvVDyBIOXcpIuH0FnZyrsV00cTPhA5XGcYQjFJR0dEkCju/SBWVFh5eLu8e1I8/PN3cOfvP//ImvBtUz7hMe5FmFpOMngYOkxlbR1SY9SAvX0fN5adzc@N/9IXw/3sAZu6Jfwndzu99np38 "Haskell – Try It Online")
[Answer]
# Racket, 63 bytes
```
(define(f l)(apply append(map(λ(x)(if(list? x)(f x)`(,x)))l)))
```
[Answer]
## Java 8 165 chars
```
import java.util.*;<T>T[]f(T[]a){List<T>l=new ArrayList<>();for(T e:a)if(e instanceof Object[])Collections.addAll(l,f((T[])e));else l.add(e);return(T[])l.toArray();}
```
Ungolfed into a class:
```
public class Q80096 {
    public static <T> T[] flatten(T[] array) {
        List<T> flattenedList = new ArrayList<>();
        for (T element : array)
            if (element instanceof Object[])
                 Collections.addAll(flattenedList, flatten((T[]) element));
            else
                flattenedList.add(element);
        return (T[]) flattenedList.toArray();
    }
}
```
This answer is based on [Jeremy Harton's approach](https://codegolf.stackexchange.com/a/80156/52713). I used it changed it in some places and created a more golf-like version.
[Answer]
# JavaScript, 17 Bytes
```
a=>eval(`[${a}]`)
```
Finally, JavaScript's type conversions can be put to some good use! Please note that this will actually output an array, but string conversion (putting it into HTML) causes it to become a comma separated list. 
If comma separated lists are acceptable output, then the following is valid:
# 7 Bytes
```
a=>""+a
```
NOTE: Snippet is broken for some reason
```
var subject = 
  a=>eval(`[${a}]`)
```
```
<input oninput="try {output.innerHTML = subject(this.value)} catch(e) {output.innerHTML='Invaild Input'}" />
<div id="output"></div>
```
[Answer]
# PHP, 73 Bytes
```
<?array_walk_recursive($_GET,function($i)use(&$r){$r[]=$i;});print_r($r);
```
[Answer]
# [Elixir](https://elixir-lang.org/), 74 bytes
```
def d(l)do l|>Stream.flat_map(fn x->if is_list(x)do d(x)else[x]end end)end
```
First Elixir answer, so can probably be golfed a bit.
[Try it online.](https://tio.run/##jY69CsMgFEb3PoU4KVix6d6t0E4tdBQJglos/gQ14NB3T5VkLhm@78LlHO7VzlabFqWNj2p2GjxTfCfpgYp9CRRyWEXgvpdXSVp6apwso5cTMgHU48UaYPPobC6odlC1oV3WvAodFGjBLUvL4f6g01wyuIbZ00@0AW23qEL8xMjAyJkJTCCB@D/caLEPhDcLBVmFfQYfWBNWr3d7a68JuehS781Yfg)
**Explanation:**
```
def d(l)do l|>            # Recursive method taking a list as input:
  Stream.flat_map(fn x->  #  Map over each item `x` of the input-list:
    if is_list(x)do       #   If `x` is a list itself:
      d(x)                #    Do a recursive call with `x`
    else                  #   Else:
      [x]                 #    Simply leave `x` unchanged
    end                   #   End of the if-else statements
  end)                    #  End of the map
end                       # End of the recursive method
```
Of course, if builtins were allowed, this could have been **25 bytes** instead:
```
fn(l)->List.flatten(l)end
```
[Try it online.](https://tio.run/##jc/NCgIhFAXg/TzFxZWCiU3r2gUTBLUXFwNqGP6Eo9DbmzK0jGlzFpfvwLna2bdNVWnjoypOwz3FR5o9qDgAmGM1ATuyO13tkplxc866H3RQFeByY6@SFziH4tkz2oANw2LP6cjpgUtCEUVk@OUalJsGTRZJutpNLEbe7Frp2Xb8UUJCdt/zi/t7Hw)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 4 bytes
```
;/ÐL
```
[Try it online!](https://tio.run/##y0rNyan8/99a//AEn////0cbGugYGehEGwOxiUFsrI6pQSwA "Jelly – Try It Online")
## Explanation
```
;/   | Reduce by concatenation
  ÐL | Loop until no further changes
```
Built-in `F` would be one byte if allowed. 
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 13 bytes
```
#~Level~{-1}&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE7876Zg@1@5zie1LDWnrlrXsFbtf0BRZl6Jg5abgr4DV3W1oYGOkYGOsUGtjgKIUwumlTwylWp1IHywQLURUAFUGEQC9UAllKprQWIgsrb2PwA "Wolfram Language (Mathematica) – Try It Online")
Un-golfed: `F[x_] := Level[x, {-1}]`
picks out the elements of the structure at the last level of its [tree form](https://reference.wolfram.com/language/ref/TreeForm.html). I'm not sure this counts as "avoiding the builtin" (which would be [`Flatten`](https://reference.wolfram.com/language/ref/Flatten.html)).
]