Java > AP-1 > commonTwo (CodingBat Solution)

Problem:

Start with two arrays of strings, a and b, each in alphabetical order, possibly with duplicates. Return the count of the number of strings which appear in both arrays. The best "linear" solution makes a single pass over both arrays, taking advantage of the fact that they are in alphabetical order.

commonTwo({"a", "c", "x"}, {"b", "c", "d", "x"}) → 2
commonTwo({"a", "c", "x"}, {"a", "b", "c", "x", "z"}) → 3
commonTwo({"a", "b", "c"}, {"a", "b", "c"}) → 3


Solution:

public int commonTwo(String[] a, String[] b) {   
  int count = 0;
  String str = "";
  for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < a.length; j++) {
      if (a[j].equals(b[i]) && !str.contains(a[j])) {
        str += a[j];
        count++;
      }
    }
  }
  return count;
}


15 comments :

  1. Here is a linear approach:

    public int commonTwo(String[] a, String[] b) {
    String sames = "";
    boolean run = true;
    int aI=0;
    int bI=0;

    while (run) {
    if (aI==a.length-1 && bI==b.length-1) {
    run=false;
    }
    if (a[aI].equals(b[bI])) {
    if (!sames.contains(a[aI])) sames += a[aI];
    if (aI b[bI].charAt(0)){
    if (bI<b.length-1) bI++;
    else if (aI < a.length - 1) aI++;
    }
    }
    }
    return sames.length();
    }

    ReplyDelete
  2. public int commonTwo(String[] a, String[] b) {
    int result = 0;
    int bcount = 0;
    String temp = "";
    for (int i=0; i0)
    {
    bcount++;
    i--;
    continue;
    }
    }
    return result;
    }

    ReplyDelete
  3. Another linear approach:

    public int commonTwo(String[] a, String[] b) {

    int count = 0;

    int ai = 0;
    int bi = 0;

    String found = "";

    while(bi < b.length && ai < a.length){
    if(a[ai].equals(b[bi]) && !a[ai].equals(found)){
    found = a[ai];
    count++;
    ai++;
    bi++;
    }else if(a[ai].equals(b[bi]) && a[ai].equals(found)){
    ai++;
    bi++;
    }else if(a[ai].compareTo(b[bi]) < 0){
    ai++;
    }else if(a[ai].compareTo(b[bi]) > 0){
    bi++;
    }
    }
    return count;
    }

    ReplyDelete
  4. public int commonTwo(String[] a, String[] b) {
    int count =0;
    String ans = "";
    for (int i = 0; i < a.length; i++)
    for (int j = 0; j<b.length; j++)
    if (a[i] == b[j] && a[i] != ans){
    ans = b[j];
    count++;
    break;
    }

    return count;
    }

    ReplyDelete
  5. public int commonTwo(String[] a, String[] b) {
    int count = 0;
    int index = 0;
    boolean found = false;
    String check[] = new String[a.length+b.length];
    for (int n = 0, m = 0; nb[m].charAt(0))
    m++;

    else if (b[m].charAt(0)>a[n].charAt(0))
    n++;

    else {
    n++;
    m++;
    }
    }
    return count;
    }

    ReplyDelete
  6. public int commonTwo(String[] a, String[] b) {
    int count = 0;
    String bCat = "";
    for (int n = 0; n < b.length; n++){
    bCat += b[n];
    }
    for (int n = 0; n < a.length-1; n++){
    if (a[n+1]!=a[n]){
    if (bCat.indexOf(a[n])!=-1) count++;
    }
    }
    if (bCat.indexOf(a[a.length-1])!=-1) count++;
    return count;
    }

    ReplyDelete
  7. public int commonTwo(String[] a, String[] b){
    int count =0;
    String found="";

    for(int i=0; i<a.length; i++){
    for(int j=0; j<b.length; j++){
    if(a[i].equals(b[j]) && b[j]!=found){
    count++;
    found=a[i];
    }
    }
    }
    return count;
    }

    ReplyDelete
  8. ArrayList < String > listA = new ArrayList< String >(Arrays.asList(a));
    listA = new ArrayList(new LinkedHashSet(listA));
    ArrayList < String > listB = new ArrayList< String >(Arrays.asList(b));
    listB = new ArrayList(new LinkedHashSet(listB));
    int count = 0 ;
    for ( int i = 0 ; i < listA.size() ; i ++ ) {
    for( int y = 0 ; y < listB.size() ; y ++ ) {
    if(listA.get(i).equals(listB.get(y))) count ++;
    }
    }
    return count;

    ReplyDelete
  9. this is the simplest linear solution

    public int commonTwo(String[] a, String[] b)
    {
    int count=0;
    int i=0,j=0; String found = "";
    while((i0 )
    j++;


    }
    return count;
    }

    ReplyDelete
  10. public int commonTwo(String[] a, String[] b) {
    int p1 = 0;
    int p2 = 0;
    int count = 0;
    String repeat = "";
    while (p1 < a.length && p2 < b.length)
    {
    if (a[p1].compareTo(b[p2]) < 0)
    {
    p1++;
    }
    else if (b[p2].compareTo(a[p1]) < 0)
    {
    p2++;
    }
    else
    {
    if (!a[p1].equals(repeat))
    {
    count++;
    repeat = a[p1];
    }
    p1++;
    p2++;
    }
    }
    return count;
    }

    ReplyDelete
  11. public int commonTwo(String[] a, String[] b) {

    Set hashSetSize = new HashSet<>();
    Collections.addAll(hashSetSize,a);

    int k = hashSetSize.size();
    hashSetSize.clear();

    Collections.addAll(hashSetSize,b);
    k += hashSetSize.size();
    Collections.addAll(hashSetSize,a);

    return k-hashSetSize.size();
    }

    ReplyDelete
  12. List list = Arrays.asList(b);
    Set set = new HashSet();
    for (String x:a){if (list.contains(x)) set.add(x);}
    return set.size();

    ReplyDelete
  13. If you randomly insist on using i+1 logic to solve this, you'd do this:

    public int commonTwo(String[] a, String[] b)
    {
    int count =0;
    if(a.length==1)
    {
    for(int z =0; z<b.length;z++)
    {
    if(a[0].equals(b[z]))
    {
    count++;
    }
    }
    }
    for(int i=0;i<a.length-1;i++)
    {
    if(a[i].equals(a[i+1]))
    {
    i+=0;
    }
    else
    {
    for(int j=0;j<b.length-1;j++)
    {
    if((b[j].equals(b[j+1]))&& j!=b.length-2)
    {
    j+=0;
    }
    else
    {
    if(a[i].equals(b[j])&&!(b[j].equals(b[j+1])))
    {
    //a[i].equals(b[b.length-1])||
    count++;
    }
    if(j==b.length-2)
    {
    if(a[i].equals(b[b.length-1]))
    {
    count++;
    }
    if(i==a.length-2)
    {
    for(int k=0; k<b.length-1;k++)
    {
    if(!(b[k].equals(b[k+1])))
    {
    if(a[i+1].equals(b[k]))
    {
    count++;
    }
    }
    }
    if(a[a.length-1].equals(b[b.length-1]))
    {
    count++;
    }

    }
    }
    }
    }
    }
    }
    return count;
    }

    ReplyDelete
  14. With Java stream and collections

    public int commonTwo(String[] a, String[] b) {
    List list1 = Arrays.stream(a).distinct().collect(Collectors.toList());
    List list2 = Arrays.stream(b).distinct().collect(Collectors.toList());
    list1.retainAll(list2);
    return list1.size();
    }

    ReplyDelete
  15. public int commonTwo(String[] a, String[] b) {

    int count = 0 ;
    int compare = 0;
    int indexB = 0;
    int indexA = 0;
    String valueToPass = "";


    while (indexA0){
    indexB++;
    }
    }

    return count;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact