compress

Category: Arrays
Author: Victoria Kirst
Book Chapter: 7.2
Problem: compress
Write a static method compress that accepts a sorted array of integers a1 as a parameter and returns a new array that
contains only the unique values of a1. The values in the new array should be ordered in the same order they originally
appeared in. For example, if a1 stores the elements {4, 4, 9, 9, 10, 10, 10, 17}, then compress(a1)
should return a new array with elements {4, 9, 10, 17}.
The following table shows some calls to your method and their expected results:
Array
int[] a1 = {2, 2, 3, 5, 5, 5};
int[] a2 = {-12, -2, 2, 8, 8, 12};
int[] a3 = {-3, 0, 0, 0, 4, 17, 32};
int[] a4 = {-92, -5, -2, -2, 0, 0, 5, 43};
int[] a5 = {1, 2, 3, 4, 5};
int[] a6 = {5, 5, 5, 5, 5, 5};
compress(a1)
compress(a2)
compress(a3)
compress(a4)
compress(a5)
compress(a6)
Returned Value
returns {2, 3, 5}
returns {-12, -2, 2, 8, 12}
returns {-3, 0, 4, 17, 32}
returns {-92, -5, -2, 0, 5, 43}
returns {1, 2, 3, 4, 5}
returns {5}
Do not modify the contents of the array passed to your method as a parameter.
You may assume the array contains at least one element.