Created
December 12, 2018 11:43
-
-
Save michalcholewinski/61c2bb37be7bb2f63dc758bf64e9e024 to your computer and use it in GitHub Desktop.
Solution - flatten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Integer[] myFlatten(Object[] input) throws IllegalArgumentException { | |
if (input == null) return null; | |
List<Integer> result = new ArrayList<Integer>(); | |
for (Object e : input) { | |
if (e instanceof Integer) { | |
result.add((Integer) e); | |
} else if (e instanceof Object[]) { | |
result.addAll(Arrays.asList(flatten((Object[]) e))); | |
} else { | |
throw new IllegalArgumentException("Wrong Input"); | |
} | |
} | |
return result.toArray(new Integer[result.size()]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment