Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method named remove which should a given value from an array of integers

ID: 3711880 • Letter: W

Question

Write a method named remove which should a given value from an array of integers. The method should return a new array which contains all of the values from the original array, except for the item which is supposed to be removed. The new array should be completely filled (and not partially filled), and thus should be declared to be the exact size needed to hold all of the integers.

The method is declared as follows, where source is the original array which is a completely filled array of integers, and key is the value which is to be removed:

public static int [] remove(int[] source, int key)

As an example, if the source array contained the following values:
     { 5, 8, 7, 8, 2 }
and key = 8, then the array which should be returned by this method is:
     { 5, 7, 2 }

The algorithm that you can use to write this method is as follows:

Count the number of times the key value appears in the source array. We wrote a search method during our lectures that will do this. You may copy and use the one from class in your program.

Create a new array that is the correct length to hold all of the numbers, minus the key value. In the example above, the original array was length 5, the key (8) was in the array twice, so if we removed the two 8's then you would be left with 3 values which would need to be stored in the new array.

Copy all of the values, except the key, from the source array into the new array. Although the new array will be completely filled when you finish, you should treat it as partially-filled until the copying is finished, thus you need to keep track of where the next empty spot in this array is located.

Return the new array

In order to pass the tests:

Your new array must be of the correct declared size

Your new array must contain all of the numbers from the original array, except for the key value

The numbers in your new array must be in the same order that they were in the original array

Pay close attention to the directions!! Make absolutely certain that you are doing exactly what the assignment askes you to do. If you don't understand the problem, make sure to ask your instructor or an assistant.

Remember to include comments for every method you create

Do not use 'break' statements to exit any loops

Explanation / Answer

public static int[] remove(int[] source,int key){ int keyCount=0; //To get the count of key occurences for(int i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote