Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating method signature in flatten array #2900

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercises/practice/flatten-array/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"aadityakulkarni",
"FridaTveit",
"jackattack24",
"jagdish-15",
"jmrunkle",
"jtigger",
"kytrinyx",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

final class Flattener {
class Flattener {

List flatten(final List nestedList) {
if (nestedList.isEmpty()) {
return new ArrayList<>();
} else {
final List result = new ArrayList();

final Object head = nestedList.get(0);
final List tail = nestedList.subList(1, nestedList.size());

if (head instanceof List) {
result.addAll(flatten((List) head));
} else {
result.add(head);
List<Object> flatten(List<?> list) {
List<Object> flattenedList = new ArrayList<>();
for (Object element: list) {
if (element instanceof List<?> listAsElement) {
flattenedList.addAll(flatten(listAsElement));
} else if (element != null) {
flattenedList.add(element);
}

result.addAll(flatten(tail));
result.removeAll(Collections.singleton(null));
return result;
}
return flattenedList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Flattener {

<T> List<T> flatten(List<T> list) {
List<Object> flatten(List<?> list) {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

Expand Down