tag:blogger.com,1999:blog-5872623838295077986.post8539842135713437737..comments2024-03-20T01:07:56.465-07:00Comments on JavaProblems.com - Free coding problems and exercises: Java > AP-1 > commonTwo (CodingBat Solution)Unknownnoreply@blogger.comBlogger13125tag:blogger.com,1999:blog-5872623838295077986.post-86738052482919839322021-05-09T00:31:12.355-07:002021-05-09T00:31:12.355-07:00public int commonTwo(String[] a, String[] b) {
S...public int commonTwo(String[] a, String[] b) {<br /> Set result = Arrays.stream(a).filter(s -> Arrays.asList(b).contains(s)).collect(Collectors.toSet());<br /> return result.size();<br />}Anonymoushttps://www.blogger.com/profile/05359114808031764899noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-71562405076772189812021-05-09T00:28:28.545-07:002021-05-09T00:28:28.545-07:00With lambda:
Set result = Arrays.stream(a).filter...With lambda:<br /><br />Set result = Arrays.stream(a).filter(s -> Arrays.asList(b).contains(s)).collect(Collectors.toSet());<br />return result.size();<br />}Anonymoushttps://www.blogger.com/profile/05359114808031764899noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-4118992590368992242020-09-23T04:29:18.110-07:002020-09-23T04:29:18.110-07:00public int commonTwo(String[] a, String[] b) {
... public int commonTwo(String[] a, String[] b) {<br /> Set setA = new HashSet<>(Arrays.asList(a));<br /> Set setB = new HashSet<>(Arrays.asList(b));<br /> return (int) setA.stream()<br /> .filter(setB::contains)<br /> .count();<br /> }Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-1980580491198078442020-09-23T04:28:22.369-07:002020-09-23T04:28:22.369-07:00public int commonTwo(String[] a, String[] b) {
... public int commonTwo(String[] a, String[] b) {<br /> Set setA = new HashSet<>(Arrays.asList(a));<br /> Set setB = new HashSet<>(Arrays.asList(b));<br /> return (int) setA.stream()<br /> .filter(setB::contains)<br /> .count();<br /> }Anonymoushttps://www.blogger.com/profile/18315574734870276996noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-86951540464711959602020-07-03T06:53:33.097-07:002020-07-03T06:53:33.097-07:00public int commonTwo(String[] a, String[] b) {
S...public int commonTwo(String[] a, String[] b) {<br /> Set setA = new HashSet<>(Arrays.asList(a));<br /> Set setB = new HashSet<>(Arrays.asList(b));<br /> int count = 0;<br /> for (String str : setA)<br /> count = setB.contains(str) ? count + 1 : count;<br /> return count;<br />}Anonymoushttps://www.blogger.com/profile/02235169415900357201noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-24787980742855767792017-05-10T12:40:49.819-07:002017-05-10T12:40:49.819-07:00So I am pretty new to java but I did get this prob...So I am pretty new to java but I did get this problem(after 30 minutes) and it involved a nested loop<br />{<br /> int count = 0;<br /> int first = 0;<br /> for (int c = 0; c<b.length;c++){<br /> if (a[0] == b[c]){<br /> count++;<br /> break;<br /> }<br /> }<br /> for (int i = 1;i<a.length;i++){<br /> if (a[i] == a[i-1]){<br /> continue;<br /> }<br /> for (int c = 0; c<b.length;c++){<br /> if (a[i] == b[c]){<br /> count++;<br /> break;<br /> }<br /> }<br /> }<br /> return count;<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-26922395496623514212017-01-25T08:42:51.372-08:002017-01-25T08:42:51.372-08:00public int commonTwo(String[] a, String[] b) {
i...public int commonTwo(String[] a, String[] b) {<br /> int count = 0;<br /> int idxA = 0;<br /> int idxB = 0;<br /> while(idxA < a.length && idxB < b.length) {<br /> if(idxA < a.length-1 && a[idxA].equals(a[idxA+1])){<br /> idxA++;<br /> continue;<br /> }<br /> if(idxB < b.length - 1 && b[idxB].equals(b[idxB+1])) {<br /> idxB++;<br /> continue;<br /> }<br /> int compare = a[idxA].compareTo(b[idxB]);<br /> if(compare < 0)<br /> idxA++;<br /> else if(compare > 0)<br /> idxB++;<br /> else {<br /> count++;<br /> idxA++;<br /> }<br /> }<br /> return count;<br />}<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-22526128395566977432016-10-05T17:41:21.077-07:002016-10-05T17:41:21.077-07:00looking for the best solution--->
public int c...looking for the best solution---><br /><br />public int commonTwo(String[] a, String[] b){<br /> int bIndex = 0;<br /> int result = 0;<br /><br /> for(int count = 0; count < a.length; count++){<br /> if(count != 0 && a[count-1].equals(a[count])) continue;<br /> if(bIndex >= b.length) break;<br /> for(int bCount = bIndex; bCount < b.length; bCount++){<br /> if(a[count].compareTo(b[bCount]) <= 0){<br /> bIndex = bCount;<br /> break;<br /> }<br /> }<br /> if(a[count].equals(b[bIndex])){<br /> result++;<br /> bIndex++;<br /> }<br /> }<br /> return result;<br /> }Anonymoushttps://www.blogger.com/profile/16909052504720222656noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-20921116374725923862016-06-21T22:51:07.790-07:002016-06-21T22:51:07.790-07:00Ok, it messed up my post. Now I know why code post...Ok, it messed up my post. Now I know why code posted by other people here wasn't compiling. I escaped the angle brackets and it seems to work now.<br /><br />public int commonTwo(String[] a, String[] b) {<br /> int alen=a.length,blen=b.length,count=0;<br /> boolean match=false;<br /> for(int i=0,j=0;i<alen&j<blen;){<br /> int cmp=a[i].compareTo(b[j]);<br /> if(cmp<0){i++;match=false;}<br /> else if(cmp>0){j++;match=false;}<br /> else {match=true;i++;j++;}<br /> if(i>0&&j>0&&i<alen&&j<blen<br /> &&a[i].compareTo(a[i-1])==0<br /> &&b[j].compareTo(b[j-1])==0)<br /> match=false;<br /> if(match)count++;<br /> }<br /> return count;<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-44806586270807711982016-06-21T22:24:23.805-07:002016-06-21T22:24:23.805-07:00I'll be the first to post a java solution usin...I'll be the first to post a java solution using one loop:<br /><br />public int commonTwo(String[] a, String[] b) {<br /> int alen=a.length,blen=b.length,count=0;<br /> boolean match=false;<br /> for(int i=0,j=0;i0){j++;match=false;}<br /> else {match=true;i++;j++;}<br /> if(i>0&&j>0&&i<alen&&j<blen<br /> &&a[i].compareTo(a[i-1])==0<br /> &&b[j].compareTo(b[j-1])==0)<br /> match=false;<br /> if(match)count++;<br /> }<br /> return count;<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-29984278706765963902015-11-02T17:20:31.404-08:002015-11-02T17:20:31.404-08:00Or...
public int commonTwo(String[] a, String[] b...Or...<br /><br />public int commonTwo(String[] a, String[] b) {<br /> // commonTwo({"a", "a", "b", "b", "c"}, {"a", "b", "b", "b", "c"}) → 3<br /> int cnt = 0;<br /> int ind = 0;<br /> <br /> for(int i = 0; i < a.length; i ++) {<br /> <br /> if (i + 1 < a.length && a[i] == a[i + 1])<br /> continue;<br /> <br /> while(ind + 1 < b.length && a[i].compareTo(b[ind]) > 0)<br /> ind ++;<br /> <br /> if (a[i].compareTo(b[ind]) == 0) {<br /> cnt ++; <br /> ind ++;<br /> }<br /> <br /> }<br /> return cnt;<br /> <br />}kithttps://www.blogger.com/profile/08782665339823243480noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-1660379293741346322015-09-06T04:11:16.021-07:002015-09-06T04:11:16.021-07:00Yes, this is not the best linear solution ( doubl...Yes, this is not the best linear solution ( double loops never makes single passes). However, your C# solution is broken.<br /><br />if (ca == a.Length || cb == b.Length) { flag = false; }<br />will trigger at the end of either of the arrays.<br />Hence since the arrays can differ in length, it will not catch a match of the last element in A with a equal element further down in B. <br />henivhttps://www.blogger.com/profile/08066080620603324982noreply@blogger.comtag:blogger.com,1999:blog-5872623838295077986.post-54856574381849168112015-08-30T11:53:24.261-07:002015-08-30T11:53:24.261-07:00You should only pass through each array once. By d...You should only pass through each array once. By doing str.contains() you actually pass through many times in the background which is very inefficient. Here is a single pass solution (done in C#, you can convert to Java easily): <br /><br />int ca = 0, cb = 0, sum = 0;<br /> bool flag = true;<br /><br /> while (flag)<br /> {<br /> if (a[ca] == b[cb]) { ca++; cb++; sum++; }<br /> else if (a[ca] < b[cb]) { ca++; }<br /> else if (a[ca] > b[cb]) { cb++; }<br /><br /> if (ca == a.Length || cb == b.Length) { flag = false; }<br /> }<br /><br /> return sum;<br />Anonymousnoreply@blogger.com