Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Checking for squared similarly of arrays in JavaScript
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and second argument respectively.
Our function should return true if and only if every element in arr2 is the square of any element of arr1 irrespective of their order of appearance.
For example, if the input to the function is:
Input
const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64];
Output
const output = true;
Using Frequency Map Approach
The most efficient approach is to create frequency maps of both arrays to compare squared values:
const arr1 = [4, 1, 8, 5, 9];
const arr2 = [81, 1, 25, 16, 64];
const isSquared = (arr1 = [], arr2 = []) => {
if (arr1.length !== arr2.length) {
return false;
}
// Create frequency maps
const freq1 = {};
const freq2 = {};
for (let num of arr1) {
freq1[num] = (freq1[num] || 0) + 1;
}
for (let num of arr2) {
freq2[num] = (freq2[num] || 0) + 1;
}
// Check if each element in arr1 has corresponding square in arr2
for (let num in freq1) {
const squared = num * num;
if (freq1[num] !== freq2[squared]) {
return false;
}
}
return true;
};
console.log(isSquared(arr1, arr2));
true
Using Sort and Compare Approach
Another approach is to sort both arrays after squaring the first array:
const isSquaredSort = (arr1 = [], arr2 = []) => {
if (arr1.length !== arr2.length) {
return false;
}
const squared = arr1.map(x => x * x).sort((a, b) => a - b);
const sorted2 = arr2.slice().sort((a, b) => a - b);
return squared.every((val, index) => val === sorted2[index]);
};
const arr1 = [4, 1, 8, 5, 9];
const arr2 = [81, 1, 25, 16, 64];
console.log(isSquaredSort(arr1, arr2));
true
Testing with Different Cases
Let's test our function with various scenarios:
// Test case 1: Valid squared arrays
const test1a = [1, 2, 3];
const test1b = [1, 4, 9];
console.log("Test 1:", isSquared(test1a, test1b));
// Test case 2: Invalid - different lengths
const test2a = [1, 2];
const test2b = [1, 4, 9];
console.log("Test 2:", isSquared(test2a, test2b));
// Test case 3: Invalid - wrong values
const test3a = [1, 2, 3];
const test3b = [1, 4, 8];
console.log("Test 3:", isSquared(test3a, test3b));
// Test case 4: Duplicate values
const test4a = [2, 2, 3];
const test4b = [4, 4, 9];
console.log("Test 4:", isSquared(test4a, test4b));
Test 1: true Test 2: false Test 3: false Test 4: true
Comparison
| Method | Time Complexity | Space Complexity | Handles Duplicates? |
|---|---|---|---|
| Frequency Map | O(n) | O(n) | Yes |
| Sort and Compare | O(n log n) | O(n) | Yes |
Conclusion
The frequency map approach is the most efficient with O(n) time complexity. It properly handles duplicates and edge cases by comparing the frequency of each number with its squared counterpart.
