Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalcholewinski/61c2bb37be7bb2f63dc758bf64e9e024 to your computer and use it in GitHub Desktop.
Save michalcholewinski/61c2bb37be7bb2f63dc758bf64e9e024 to your computer and use it in GitHub Desktop.
Solution - flatten
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