logo Practice-It logo

compress

Language/Type: Java arrays
Author: Roy McElmurry

Write a static method named compress that accepts an 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 {10, 10, 9, 4, 10, 4, 9, 17}, then compress(a1) should return a new array with elements {10, 9, 4, 17}.

The following table shows some calls to your method and their expected results:

Arrays Returned Value
int[] a1 = {5, 2, 5, 3, 2, 5}; compress(a1) returns {5, 2, 3}
int[] a2 = {-2, -12, 8, 8, 2, 12}; compress(a2) returns {-2, -12, 8, 2, 12}
int[] a3 = {4, 17, 0, 32, -3, 0, 0}; compress(a3) returns {4, 17, 0, 32, -3}
int[] a4 = {-2, -5, 0, 5, -92, -2, 0, 43}; compress(a4) returns {-2, -5, 0, 5, -92, 43}
int[] a5 = {1, 2, 3, 4, 5}; compress(a5) returns {1, 2, 3, 4, 5}
int[] a6 = {5, 5, 5, 5, 5}; compress(a6) returns {5}
int[] a7 = {}; compress(a7) returns {}
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.