Coding: Check If Two String Arrays are Equivalent in Javascript | LeetCode problem #1662

Monu Kumar Modi
JavaScript in Plain English
4 min readFeb 22, 2023

--

We will be discussing multiple solutions.

Solution 1:

The arrayStringsAreEqual the function takes two parameters, word1 and word2, which are arrays of strings. The function concatenates all the strings inword1 and all the strings inword2, and then compares the resulting concatenated strings. If the two concatenated strings are equal, the function returnstrue. Otherwise, it returnsfalse.

Let’s see an example of how this function works. Suppose we have two arrays, arr1 and arr2, as follows:

const arr1 = ["abc", "def", "ghi"];
const arr2 = ["abcdef", "ghi"];

To check if these two arrays are equal, we can call the arrayStringsAreEqual function with arr1 and arr2 as arguments:

console.log(arrayStringsAreEqual(arr1, arr2)); // true

The arrayStringsAreEqual function concatenates all the strings in arr1 to create the string "abcdefghi", and concatenates all the strings in arr2 to create the same string "abcdefghi". Since the two concatenated strings are equal, the function returns true.

Let’s look at another example where the two arrays are not equal:

const arr3 = ["foo", "bar", "baz"];
const arr4 = ["foo", "bar"];
console.log(arrayStringsAreEqual(arr3, arr4)); // false

The arrayStringsAreEqual function concatenates all the strings in arr3 to create the string "foobarbaz", and concatenates all the strings in arr4 to create the string "foobar". Since these two concatenated strings are not equal, the function returns false.

Complete Code:

var arrayStringsAreEqual = function(word1, word2) {
var str1 = "", str2 = ""
for(let i =0; i<word1.length; i++){
str1 += word1[i]
}
for(let i =0; i<word2.length; i++){
str2 += word2[i]
}
return str1 === str2 ? true : false
};

In summary, the arrayStringsAreEqual the function provides a simple way to check whether two arrays of strings have the same contents in JavaScript. While this function may not be the most efficient approach for large arrays, it can be a useful tool in situations where the arrays are small and the performance of the function is not a concern.

Solution 2: Using the join() Method

function arrayStringsAreEqual(word1, word2) {
return word1.join("") === word2.join("");
}

This implementation uses the join method to concatenate all the strings in each array, and then compares the resulting concatenated strings. If the two concatenated strings are equal, the function returns true. Otherwise, it returns false.

Let’s see an example of how this implementation works. Suppose we have the same arrays as before:

const arr1 = ["abc", "def", "ghi"];
const arr2 = ["abcdef", "ghi"];
console.log(arrayStringsAreEqual(arr1, arr2)); // true

const arr3 = ["foo", "bar", "baz"];
const arr4 = ["foo", "bar"];
console.log(arrayStringsAreEqual(arr3, arr4)); // false

In conclusion, the arrayStringsAreEqual the function can be implemented in multiple ways, including using built-in JavaScript methods like join to simplify the code.

Solution 3: Using reduce() Method

function arrayStringsAreEqual(word1, word2) {
const concat1 = word1.reduce((str, curr) => str + curr, "");
const concat2 = word2.reduce((str, curr) => str + curr, "");
return concat1 === concat2;
}

In this implementation, the reduce method is called on each array to concatenate all the strings in the array into a single string. The reduce the method takes a callback function that takes two arguments: an accumulator and the current element of the array being processed. In each iteration, the callback function concatenates the current element to the accumulator. The initial value of the accumulator is an empty string.

Once the two concatenated strings are created, the function compares them to determine if they are equal. If they are equal, the function returns true. Otherwise, it returns false.

Solution 4: Using every() Method

function arrayStringsAreEqual(word1, word2) {
return word1.join("").length === word2.join("").length &&
word1.every((val, i) => val === word2[i]);
}

In this implementation, the join method is called on both arrays to concatenate all the strings in each array into a single string. Then, the length property of the concatenated strings is compared to ensure that the two strings have the same length. If the lengths of the concatenated strings are not equal, the function returns false.

If the lengths of the concatenated strings are equal, the every method is used to compare each string in both arrays. The every method takes a callback function that compares each element in both arrays. The callback function returns false as soon as it finds a mismatched element. If all elements are equal, the function returns true.

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

Build awareness and adoption for your tech startup with Circuit.

--

--