tag:blogger.com,1999:blog-694855878384792308.post3434067012919091411..comments2024-03-21T06:26:49.387-07:00Comments on Java67: How to remove a number from an Integer Array in Java? [Example Tutorial]javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-694855878384792308.post-69441350454556743812020-03-11T06:08:51.125-07:002020-03-11T06:08:51.125-07:00Hello Raju, because adding and removing element fr...Hello Raju, because adding and removing element from linked list is O(1) operation and they also don't need to allocate memory in advance like array which will required re-allocation if we have large array. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-79760879366707608662020-03-10T23:57:00.829-07:002020-03-10T23:57:00.829-07:00Hello sir why you used LinkedList to add the
non m...Hello sir why you used LinkedList to add the<br />non matching elements in array Rajuhttps://www.blogger.com/profile/14044748050551491411noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-64333522558653611592020-01-31T04:50:00.487-08:002020-01-31T04:50:00.487-08:00there is no direct way to remove elements from arr...there is no direct way to remove elements from array than creating another array and copying the remaining elements, so you need to just copy elements which are not removed. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-26206054506000482822020-01-31T02:20:03.800-08:002020-01-31T02:20:03.800-08:00Hi, if you hae int set {0,1,2,3,4,5,6,7,8,9} and y...Hi, if you hae int set {0,1,2,3,4,5,6,7,8,9} and you want to remove the second[2] number. then update the int set to {0,1,3,4,5,6,7,8,9} and then again remove the second[2] number(3) to get to the int set {0,1,4,5,6,7,8,9}. how do you do that?Anonymoushttps://www.blogger.com/profile/11731600593647953730noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-70499458452697015252016-08-20T07:59:50.135-07:002016-08-20T07:59:50.135-07:00Hi Javin,
I guess It can be solved O(n).
Please ch...Hi Javin,<br />I guess It can be solved O(n).<br />Please check below solution.<br /><br />-----------CODE----------------<br />public class DeleteAnyElementFromArray {<br /><br /> public static void main(String[] args) {<br /> // TODO Auto-generated method stub<br /> Integer[] ar = {12,3,5,1,12,6,9,12,15,-8,-1,12,-6,-3,5,7};<br /> int len = ar.length;<br /> System.out.println(len);<br /> int keyToDel =12;<br /> int j =0; //keeping J to count the freq of the item to be deleted<br /> int i=0;<br /> for(;i<len;i++){//O(n)<br /> if(ar[i] != keyToDel){<br /> ar[i-j] = ar[i];//I'm keeping the running index in check by reducing <br /> } //the increment index by the freq of the item to be deleted<br /> else <br /> j++; //increment when the element is "item to be deleted"<br /> }<br /> for(int k = i-j+1;k<i;k++){//O(n)<br /> ar[k] =null; //this loop shall perform worst when all the elements are same and they all have to be deleted<br /> }<br /> for (int k =0 ;k<i-j;k++){ //printing till the <br /> System.out.print(ar[k]+",");<br /> }<br /> }<br /><br />}<br />Anonymousnoreply@blogger.com