How to determine whether an array is symmetrical in javascript

PHPz
Release: 2023-04-24 16:10:15
Original
850 people have browsed it

In JavaScript, determining whether an array is symmetrical is a common problem. A symmetric array means that starting from the central axis of the array and extending to both ends, it can be found that the elements at the corresponding positions are equal.

So how to determine whether an array is symmetrical? The following are two common methods:

Method 1: Use a loop

First find the center position of the array, which is half the length of the array. Next, use a loop to compare whether the elements at corresponding positions are equal starting from the starting position of the array until the center position. If there are unequal elements, you can return false, otherwise return true.

The following is a sample code:

function isSymmetric(arr) { const len = arr.length; for (let i = 0; i < len / 2; i++) { if (arr[i] !== arr[len - 1 - i]) { return false; } } return true; }
Copy after login

The time complexity of this method is O(n/2), which is O(n), because only half of the array needs to be traversed.

When using this method, please note that if the array length is an odd number, the element at the center position does not need to be compared, because it must be symmetrical.

Method 2: Use the reverse method

The array object in JavaScript has a reverse method, which can reverse the array. If an array is symmetric, then its inverted result must be equal to the original array.

The following is a sample code:

function isSymmetric(arr) { return arr.join('') === arr.reverse().join(''); }
Copy after login

The time complexity of this method is O(n), because only the join method and the reverse method need to be called twice.

When using this method, please note that the original array will be changed, because the reverse method will modify the original array. If you don't want to modify the original array, you can use the slice method to create a copy and reverse it.

No matter which method is used, determining whether an array is symmetrical is a simple question, but it can help us deepen our understanding of JavaScript arrays. When we need to perform various operations on arrays, having a clear understanding of the symmetry of the internal elements of the array can improve our programming efficiency.

The above is the detailed content of How to determine whether an array is symmetrical in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!