How to write function for deleting array elements in java ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to write function for deleting array elements in java ?

We have array of our own choice with some duplicate elements. For example {10,20,10,30,40,10,50} Now we have to define two functions Function 1 : For deleting an element (user input) in the array. This function deletes first occurence of that element (not all same elements) and shifts other array elements reducing array size Function 2 : This functions deletes all the elements (all the same elements) , shifts the array. I have been working on this but still not able to solve it efficiently especially case 2. It will be a great help if someone looks into this. THANKS !!!

30th Sep 2021, 3:38 AM
Viraj
Viraj - avatar
1 Answer
+ 2
I don't know java. But I can give you general programming approach. You can have another array to store the elements after deleting the array. For that just iterate over the given array, check for the element to be deleted... Don't just put that element in the new array. flag = 1 Function 1(delElm != arr[...] && flag ==1) Set flag to zero after the first element is removed so that it doesn't delete next occurrence. Function 2(delElm != arr[...] )
30th Sep 2021, 8:28 AM
Kashyap Kumar
Kashyap Kumar - avatar