-2

There are ways in which we can reduce the array size using a new array. But I want to know how we can do it without using an additional array.

3 Answers3

3

Once an array is created, you cannot change the size. You can either create a new Array, or use an ArrayList (internally, however, the ArrayList creates a new array, but this is hidden from you).

MusicMaster
  • 549
  • 4
  • 14
0

did you mean to reduce element size of array ? if yes, i think the only way to do it using copy the original array to new array that have smaller size.

0

Arrays are static in size. When you initialize an array with a given size, it will always have that size. Think of an Array as an egg carton. It may or may not have eggs in each of its slots, but it will always have 12 slots (or 6 or 18 or however many).

As far as deleting duplicates, you can replace duplicates you encounter with null, which makes the slot "empty".

public static void deleteDuplicates(Character[] arr){
    for(int i = 0; i < arr.length; i++){
        for(int j = 0; j < i; j++){
            if(arr[j] != null && arr[j].equals(arr[i]){
                //Found duplicate - delete duplicate and stop searching
                arr[i] = null;
                break;
        }
    }
}
Mshnik
  • 7,032
  • 1
  • 25
  • 38