public static NSArray flatten(NSArray originalArray) { if (originalArray == null || originalArray.count() < 1) { return originalArray;
public static NSArray flatten(NSArray originalArray, boolean filterDuplicates) { NSArray flattenedArray = flatten(originalArray);  if (filterDuplicates) { return arrayWithoutDuplicates(flattenedArray); } else { return flattenedArray;
    public static NSArray flatten(NSArray originalArray) {        if (originalArray == null || originalArray.count() < 1) {            return originalArray;        }                NSMutableArray newArray = null;  // Not gonna create a new array if we don't actually need to flatten        for (int i = 0; i < originalArray.count(); i++) {            Object element = originalArray.objectAtIndex(i);            if (element instanceof NSArray) {                if (newArray == null) {                    // Turns out we actually need to flatten                    newArray = new NSMutableArray();                    for (int backfillIndex = 0; backfillIndex < i; backfillIndex++) {                        // backfill any singles we put off copying                        newArray.addObject(originalArray.objectAtIndex(backfillIndex));                    }                }                                NSArray flattenedChildArray = flatten((NSArray)element);                newArray.addObjectsFromArray(flattenedChildArray);            } else if (newArray != null) {                newArray.addObject(element);            }  // Otherwise let's put off copying the elment, the backfill section above will take care of it.        }                // CLEANUP: Arguably safer to return the immutable array we are declared as returning        return (newArray != null ? newArray : originalArray);    }