Home > Back-end >  I am trying to return true if the two arrays have common values and return False otherwise
I am trying to return true if the two arrays have common values and return False otherwise

Time:12-14

I am trying to return true if the two arrays have common values and return False otherwise. the problem is when running this code I found that arrays are not as declared. one of the two arrays contains the values of both arrays

Here is the Code:

#include<bits/stdc  .h>
using namespace std;

bool commonValues(char arr1[], char arr2[]){

    for (int i = 0; i < strlen(arr1); i  ){
        for(int j = 0; j < strlen(arr2); j  ){
            if (arr1[i] == arr2[j]){
                return true;
            }
        }

    }
    return false;

}

int main(){
    char arr1[] = {'a', 'b', 'c', 'd', 'z', 'x', 'k', 'l'};
    char arr2[] = {'e', 'f', 'g', 'h'};
    for (int i = 0; i < strlen(arr2); i  ){
    }

    cout<<commonValues(arr1, arr2)<<endl;

    return 0;
}

CodePudding user response:

Here is a modern C variant that will work for you:

#include <iostream>
#include <string_view>
#include <unordered_set>

using namespace std::string_view_literals;

bool commonValues(std::string_view a, std::string_view b) {
    std::unordered_set<char> chars_in_a;
    for (auto c : a) {
        chars_in_a.insert(c);
    }
    
    for (auto c : b) {
        if (chars_in_a.contains(c)) {
            return true;
        }
    }
    return false;
}

int main() {
    constexpr auto str_a = "abcdzxkl"sv;
    constexpr auto str_b = "efgh"sv;
    constexpr auto str_c = "jptxo"sv;

    std::cout << str_a << " contains at least one character of " << str_b << ": ";
    std::cout << std::boolalpha << commonValues(str_a, str_b) << std::endl;

    std::cout << str_a << " contains at least one character of " << str_c << ": ";
    std::cout << std::boolalpha << commonValues(str_a, str_c) << std::endl;
}

Advantages:

  • Uses strings/string views rather than arrays and pointers. Avoid using pointers and raw arrays in C unless you need to (e.g. when performing some very low level operations). std::string is usually the way to go, and if you are merely inspecting a string (like in your function commonValues), you'd usually want to pass a string_view for efficiency. In your case the string never changes, so you can even initialize with a constexpr string_view.
  • Runs in O(n) rather than O(n*m) (where n is the longer string length). Instead of having a nested loop over both arrays, we simply store what we've "seen" so far into a (unordered) set, then we can simply query in O(1) (avg.) whether an element of the second string is included in the first one. We are sacrificing O(n) of memory here, but usually you'll be hitting the runtime barrier way sooner in such algorithms.

Please add a comment in case further clarification is required.

CodePudding user response:

One way to do this would be by using template non-type parameters as shown below:

Version 1: Using templates

#include<iostream>

//arr1 and arr2 are reference to array of size M and N respectively
template<std::size_t M, std::size_t N>
bool commonValues(char (&arr1)[M], char (&arr2)[N]){

    for (int i = 0; i < M; i  ){//M USED HERE
        for(int j = 0; j < N; j  ){//N USED HERE
            if (arr1[i] == arr2[j]){
                std::cout<<"common element found: "<<arr1[i]<<std::endl;
                return true;
            }
        }
    }
    return false;

}

int main(){
    char arr1[] = {'a', 'b', 'c', 'd', 'z', 'x', 'k', 'l'};
    char arr2[] = {'e', 'f', 'g', 'h'};

    std::cout<<commonValues(arr1, arr2)<<std::endl;

    return 0;
}

Another way would be to pass the size of the arrays to the function as shown below.

Version 2: Passing the size of array as arguments

#include<iostream>
#include <string> //needed for std::size with C  17
//the 2nd and the 4th parameters are the size of the arrays
bool commonValues(char *arr1, std::size_t M, char *arr2, std::size_t N){

    for (int i = 0; i < M; i  ){//M USED HERE
        for(int j = 0; j < N; j  ){//N USED HERE
            if (arr1[i] == arr2[j]){
                std::cout<<"common element found: "<<arr1[i]<<std::endl;
                return true;
            }
        }
    }
    return false;

}

int main(){
    char arr1[] = {'a', 'b', 'c', 'd', 'z', 'x', 'k', 'l'};
    char arr2[] = {'e', 'f', 'g', 'h'};
    //note that we are passing 4 arguments this time.
    std::cout<<commonValues(arr1, std::size(arr1), arr2, std::size(arr2))<<std::endl;

    return 0;
}
  • Related