0

How to remove from an array using recursion in Java?

the array can be in any length and with any integers.

You may not use loops (while,for etc.)

for example

[2][4][2][0][9]

should print (2 4 0 9)

thanks for your assistance

3
  • 3
    What have you tried so far? (This looks like homework. If it is, please tag it as such.) Commented Sep 7, 2011 at 7:36
  • 3
    What element should be removed? A random one? You have to be more explicit. Commented Sep 7, 2011 at 7:37
  • 1
    What is to be removed? Duplicates? Please refine the question. Maybe you colud also state that it is a homework, if it is. Commented Sep 7, 2011 at 7:39

3 Answers 3

4

You can't remove elements from an array in Java. Not with recursion, not with loops.

2

I am assuming here you don't actually want to remove the element [as @aioobe mentioned, it cannot be done], but you actually want to: "print all elements in the array, without duplicates". [I assume it from the example, you mention you want to print the specified output].

The way to do it is maintain a Set containing all elements you have already encountered, and print an element if and only if you encounter it and it is not in the set.

The recursion part will probably be: always handle the i element (arr[i]), and stop when i == arr.length. don't forget to increase i in each recursive call.

should look something like that:

public static void printNoDupes(int[] arr,int i,Set<Integer> seen) {
    if (i == arr.length) return;
    if (!seen.contains(arr[i])) {
        System.out.print(arr[i] + " ");
        seen.add(arr[i]);
    }
    printNoDupes(arr,i+1,seen);
}
public static void printNoDupes(int[] arr) { 
    printNoDupes(arr,0,new HashSet<Integer>());
    System.out.println();
}
2
  • If using Set is allowed, the all you need is ` System.out.println(new HashSet<Integer>(Arrays.asList(new Integer[] {2, 4, 2, 0, 9 })).toString());` Commented Sep 7, 2011 at 8:49
  • @MacroS: true, but the OP specifically asked for a recursive solution. Commented Sep 7, 2011 at 9:57
1

Sort the array and then use recursion to remove element if it is same as the element previously found element / argument that indicates previous unique element.

And as others said, you can't remove elements from original array, you need to copy the elements that should be kept to a new array.

2
  • "...you need to copy the elements that should be kept to a new array"- but what dimension would this array have? Commented Sep 8, 2011 at 10:42
  • you can either initialize the new array's size same as the original one and use some special element to indicate its end or you can count the unique elements with the first call of the function then allocate a new array with that size, then store unique elements to new array in the second call. Commented Sep 8, 2011 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.