tag:blogger.com,1999:blog-8712770457197348465.post2087067677553359413..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to Remove Duplicates from Array Without Using Java Collection API? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger63125tag:blogger.com,1999:blog-8712770457197348465.post-7316011378958031182022-02-26T03:17:20.241-08:002022-02-26T03:17:20.241-08:00package StringQ;
import java.util.HashSet;
publi...package StringQ;<br /><br />import java.util.HashSet;<br /><br />public class MixStrringChar {<br /> public static void main(String[] args) {<br /> <br /><br /> String st="suunnil";<br /> <br /> int a=st.length()-1;<br /> for(int i=0;i<a;i++) {<br /> if(st.charAt(i)!=st.charAt(i+1)) {<br /> int s=st.charAt(i);<br /> System.out.print(st.charAt(i));<br /> }<br /> }<br />}}sunil jangirhttps://www.blogger.com/profile/16824933651703390058noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2746394685190587442021-05-17T04:52:55.601-07:002021-05-17T04:52:55.601-07:00Cant we use this to remove the duplicates
public...Cant we use this to remove the duplicates<br /><br /> public void removeDuplicates(int[] a){<br /> Set set = new HashSet();<br /><br /> for(int i=0;i<a.length;i++){<br /> set.add(a[i]);<br /> }<br /><br /> Object[] arr = set.toArray();<br /><br /> System.out.println(Arrays.toString(arr));<br /><br /> }Mohamed Rifadhttps://www.blogger.com/profile/07285162615784680010noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27853312025859825692021-01-27T20:44:02.642-08:002021-01-27T20:44:02.642-08:00Nice solution, keep it up. Nice solution, keep it up. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87021432469861517312021-01-22T19:14:05.703-08:002021-01-22T19:14:05.703-08:00Without sorting:
int[] a= {1,4,2,3,3,4,4,5};
i...Without sorting:<br /><br /><br />int[] a= {1,4,2,3,3,4,4,5};<br /> int counter=0;<br /> boolean f=false;<br /> <br /> //count how much we have different numbers<br /> for (int i=0;i-1;g--) {<br /> if(a[i]==a[g]) {<br /> f=false;break;<br /> }else {f=true;} <br /> } <br /> } <br /> if(f==true) {<br /> counter++;<br /> }<br /> }<br /> <br /> //create new Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5266078008652004832020-07-25T22:38:05.970-07:002020-07-25T22:38:05.970-07:00Yes, but time complexity will be increased by KYes, but time complexity will be increased by Kjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65105104339953820922020-07-17T10:59:28.059-07:002020-07-17T10:59:28.059-07:00What about this, can't we overcome the space w...What about this, can't we overcome the space waste using this. Time complexity will still be O(n)<br /><br />public static void main(String[] args) {<br /> int[] arr = { 1, 2, 3, 3, 4, 5, 6, 2 };<br /> int[] duplicates = findDuplicates(arr);<br /> int[] unique = removeDuplicates(arr);<br /> System.out.println(Arrays.toString(duplicates));<br /> System.out.println(Arrays.toString(unique))Kuldeep Singhhttps://www.blogger.com/profile/11502661592790499059noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8958132078762815312020-07-14T20:56:26.703-07:002020-07-14T20:56:26.703-07:00Nice one, keep it upNice one, keep it upjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59939078148941904022020-07-08T21:32:56.487-07:002020-07-08T21:32:56.487-07:00int [] intArray = {1,2,5,6,99,88,99,25,33,33,66,41...int [] intArray = {1,2,5,6,99,88,99,25,33,33,66,41,41};<br /> LinkedHashSet lhs = new LinkedHashSet();<br /> for(Integer in : intArray)<br /> {<br /> lhs.add(in);<br /> }<br /> Object[] updatedArray = lhs.toArray();<br /> for(int i=0;i<updatedArray.length;i++)<br /> {<br /> System.out.println(updatedArray[i]);<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81092494304502881882020-04-09T05:27:15.031-07:002020-04-09T05:27:15.031-07:00Hello arpit, can you explain the code little bit f...Hello arpit, can you explain the code little bit for people who are not familiar with bit wise manipulation?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45533547819787690422020-04-08T20:11:45.263-07:002020-04-08T20:11:45.263-07:00Use bit manioulation
int counter =0;
if(counter...Use bit manioulation <br /><br />int counter =0;<br /><br />if(counter & counter << 1 << val > 0 //then its duplicate<br /><br />else {<br />counter != (counter << 1 << val )<br />}arpithttps://www.blogger.com/profile/14105438223736068548noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36692916238664032612020-03-09T07:35:20.733-07:002020-03-09T07:35:20.733-07:00Hello @Kennedy, currently you are doing testing in...Hello @Kennedy, currently you are doing testing in your main method, which is just testing one case. It would be better if you encapsulate logic in one class and then another class for JUnit test and then create multiple test method for different test cases like empty array, array without duplicates, array with few duplicates, and array with all duplicates. Hope this is clear. For an example, javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24798243124769687862020-03-06T12:22:09.088-08:002020-03-06T12:22:09.088-08:00@ javin paul : When asked that question "how ...@ javin paul : When asked that question "how about refactoring this program and separate testing as Unit test?" What exactly am I supposed to do? <br />Kennedyhttps://www.blogger.com/profile/02819514168786468014noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63986629342007110722020-03-06T11:54:52.649-08:002020-03-06T11:54:52.649-08:00@Javin paul : I think that's exactly what I ne...@Javin paul : I think that's exactly what I need to do<br />Let me also take this opportunity to thank you for creating this wonderful developers community. I have online code challenge with a big company and thus I am practicing here... Be blessed :)<br />Kennedyhttps://www.blogger.com/profile/02819514168786468014noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56216044126502490792020-03-06T04:44:48.040-08:002020-03-06T04:44:48.040-08:00great job, how about refactoring this program and ...great job, how about refactoring this program and separate testing as Unit test?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55309879587468801822020-03-05T08:38:42.860-08:002020-03-05T08:38:42.860-08:00/** How to Remove Duplicates from Array Without Us.../** How to Remove Duplicates from Array Without Using Java Collection API<br />* @author Kennedy <br />* @ email: jambotechsolutions@gmail.com<br />*/<br /> <br />import java.util.Arrays; <br /> <br />public class GFG { <br /> <br /> // Function to remove the element <br /> public static int[] removeTheElement(int[] arr, <br /> int index) <br />Kennedyhttps://www.blogger.com/profile/02819514168786468014noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78504824877345266732020-02-21T16:42:32.138-08:002020-02-21T16:42:32.138-08:00public class RemoveDuplicates {
public static vo...public class RemoveDuplicates {<br /><br /> public static void main(String[] args) {<br /> System.out.println("To remove duplicates!");<br /> <br /> int[] arr = {1,4, 2, 1,3,3,2};<br /> removeDup(arr);<br /> <br /> }<br /><br /> public static void removeDup(int[] arr)<br /> {<br /> <br /> //Instantiate a List to store all the values of array elements<br /> List list = new Unknownhttps://www.blogger.com/profile/04774059706187441714noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78925022427120076182020-02-04T05:16:51.866-08:002020-02-04T05:16:51.866-08:00Hello @Unknwn, can you elaborate please, beginner ...Hello @Unknwn, can you elaborate please, beginner can also use this solution. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12956606037004847842020-02-03T20:08:11.929-08:002020-02-03T20:08:11.929-08:00sir please suggest your solutions for begineers al...sir please suggest your solutions for begineers also .Anonymoushttps://www.blogger.com/profile/09550678211592958938noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59347839387163366092019-11-20T14:51:34.392-08:002019-11-20T14:51:34.392-08:00public static void main(String args[]) {
int[] a...public static void main(String args[]) {<br /> int[] arr = { 2,1, 2, 3, 4, 5,3, 6, 7, 8, 9, 10 };<br /> int exDupNum;<br />Arrays.sort(arr);<br />System.out.println(arr[0]);<br /> for (int i = 1; i < arr.length; i++) {<br /> <br /> if ((arr[i] != arr[i-1])) {<br /> exDupNum=arr[i];<br /> System.out.println(exDupNum);<br /><br /> }<br /><br /> }<br /><br /> }<br />Anonymoushttps://www.blogger.com/profile/12033983669164594035noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53788190400771988482019-11-20T13:01:43.928-08:002019-11-20T13:01:43.928-08:00public static void main(String args[]) {
int[] a...public static void main(String args[]) {<br /> int[] arr = { 1, 2, 3, 4, 5, 6, 4, 3, 8,8,9 };<br /> int dupNum;<br /> Arrays.sort(arr);<br /> for (int i = 1; i < arr.length; i++) {<br /><br /> if (arr[i] == arr[i - 1]) {<br /> dupNum = arr[i];<br /> System.out.println(dupNum);<br /><br /> }<br /> }<br /><br /> }Anonymoushttps://www.blogger.com/profile/12033983669164594035noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66861951200685102492019-10-10T02:09:25.687-07:002019-10-10T02:09:25.687-07:00import java.lang.reflect.Array;
import java.util.A...import java.lang.reflect.Array;<br />import java.util.Arrays;<br /><br />public class DuplicateNumber {<br /> public static void main(String[] args){<br /> int[] numbers ={1,3,2,1,4,5,4};<br /> int size = numbers.length;<br /> int[] newnum = new int[size];<br /><br /><br /> for (int i= 0; i<size; i++){<br /><br /> for(int j=i+1;j<size;j++){<br /> if(Sumedhahttps://www.blogger.com/profile/06950770229829618335noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16115831837467219652019-04-19T18:52:13.302-07:002019-04-19T18:52:13.302-07:00test which shows that solution works for different...test which shows that solution works for different input e.g. array without duplicates, array with one duplicates, array with multiple duplicates, array with just duplicates, empty array, array with one element etc. Nowadays, Interviewer are also focusing on TDD (Test driven development) which encourage to write test first than solution. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53136599265066348522019-04-19T10:07:20.534-07:002019-04-19T10:07:20.534-07:00Thank you @javin, but what kind of unit test are y...Thank you @javin, but what kind of unit test are you talking about exactly?Sagar Ravalhttps://www.blogger.com/profile/07414064563536574552noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85332787109844450962019-04-18T19:50:27.585-07:002019-04-18T19:50:27.585-07:00Good Job @Sagar, how about writing some unit tests...Good Job @Sagar, how about writing some unit tests?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74443310753943533902019-04-18T06:26:19.896-07:002019-04-18T06:26:19.896-07:00// Finding Duplicates in Array(Sorted)
public cl...// Finding Duplicates in Array(Sorted)<br /><br /><br />public class _02<br />{<br /> public static void main(String[] args)<br /> {<br /> int A[]={1,2,2,3,4,4,4};<br /> int B[]={0,0,0,0};<br /> int i,j;<br /> <br /> //printing array(A)<br /> for(i=0;i<A.length;i++)<br /> {<br /> System.out.println(A[i]);<br /> }<br />Sagar Ravalhttps://www.blogger.com/profile/07414064563536574552noreply@blogger.com