Contents

Deleting Elements of an Array (with.Java)

   Nov 23, 2023     2 min read

This is a recap of the “Delete elements of an array” problem.

We’re going to learn by solving the coding test problem, reflecting on how we solved it, and exploring other ways to solve it.

Let’s start with the problem

Problem

We have an array of integers, arr and delete_list.

Write a solution function that deletes all of the elements in delete_list from arr and returns an array with the remaining elements in the same order as they were in arr.

Example input and output

arrdelete_listresult
[293, 1000, 395, 678, 94][94, 777, 104, 1000, 1, 12][293, 395, 678]
[110, 66, 439, 785, 1][377, 823, 119, 43][110, 66, 439, 785, 1]

My solution to the problem

import java.util.ArrayList;
class Solution {
    public int[] solution(int[] arr, int[] delete_list) {
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        int count = 0;
        for(int ele1: arr){
            for(int ele2: delete_list){
                if(ele1 == ele2){
                    count++;
                }
            }
            if(count == 0){
                arrList.add(ele1);
            }
            } count = 0;

        int[] answer = new int[arrList.size()];

        for(int i = 0; i < arrList.size(); i++){
            answer[i] = arrList.get(i);
        }
        return answer;
    }
}

Solution

ArrayList arrList = new ArrayList();: Creates an ArrayList arrList to store the integers. This list will be used to store all the elements except the ones to be deleted.

int count = 0;: Initialize the variable count, which will store the number of elements that match the element to be deleted.

for(int ele1 : arr) : Iterates over the input array arr, examining each element.

for(int ele2 : delete_list) : Iterate over the list to delete, delete_list, examining each element.

if(ele1 == ele2) : If the current ele1 and ele2 match:

Increment count. if(count == 0) : If count is 0 (i.e., ele1 is not in delete_list):

Add ele1 to arrList. count = 0;: initialize count to prepare for examining the next ele1 element.

int[] answer = new int[arrList.size()];: Create an array answer with size equal to the size of arrList.

for(int i = 0; i < arrList.size(); i++) : copy the elements of arrList into the array answer.

return answer;: Returns an array answer consisting of the elements not included in the list delete_list to be deleted.

This code collects the elements not included in the list to delete delete_list from the input array arr, creates a new array with them, and returns it.