Checking the equality of array elements (sequence dependent) in JavaScript

In this article, we'll learn how to check the equality of array elements in a sequence-dependent manner. This means comparing arrays element by element at the same index positions to find matching values or count similarities.

Input-Output Scenario

Let's look at how sequence-dependent comparison works with two arrays:

Array1 = [2, 5, 6, 7, 9, 0];
Array2 = [3, 5, 0, 8, 9, 4];

Output = [5, 9]

In the above example, elements 5 and 9 appear at the same index positions (index 1 and 4) in both arrays, making them sequence-dependent matches.

Method 1: Finding Equal Elements

This approach iterates through both arrays and collects elements that match at the same index positions.

<!DOCTYPE html>
<html>
<head>
   <title>Checking the equality of array elements (sequence dependent) in JavaScript</title>
   <button onClick="func()">Click!</button>
   <p id="para"></p>
</head>
<body>
   <script>
      function func() {
         function EqualElements(array1, array2) {
            let EmpArr = [];
            for (let i = 0; i < array1.length; i++) {
               if (array1[i] !== array2[i]) {
                  continue;
               } else {
                  EmpArr.push(array1[i]);
               };
            };
            return EmpArr;
         };
         const array1 = [10, 22, 30, 44, 50, 66, 70];
         const array2 = [10, 33, 20, 44, 55, 60, 70];
         document.getElementById("para").innerHTML = "Equal array elements (sequence dependent): " + EqualElements(array1, array2);
      };
   </script>
</body>
</html>
Equal array elements (sequence dependent): 10,44,70

Method 2: Counting Similar Elements

This approach counts how many elements match at the same index positions instead of collecting them.

<!DOCTYPE html>
<html>
<head>
   <title>Checking the equality of array elements (sequence dependent) in JavaScript</title>
   <button onClick="func()">Click!</button>
   <p id="para"></p>
</head>
<body>
   <script>
      const array1 = [10, 22, 30, 44, 50, 66, 70];
      const array2 = [10, 33, 20, 44, 55, 60, 70];
      function func() {
         function EqualElements(array1, array2) {
            let count = 0;
            var i = 0;
            while (i < array1.length) {
               if (array1[i] == array2[i]) {
                  count++;
               }
               i++;
            }
            return count;
         };
         document.getElementById("para").innerHTML = "Count of similar elements in sequence: " + EqualElements(array1, array2);
      };
   </script>
</body>
</html>
Count of similar elements in sequence: 3

Comparison of Methods

Method Return Type Use Case
Finding Equal Elements Array of matching elements When you need the actual matching values
Counting Elements Number of matches When you only need to know how many elements match

Key Points

  • Both methods compare arrays at the same index positions (sequence-dependent)

  • The first method collects matching elements into a new array

  • The second method simply counts the matches using a counter variable

  • Arrays should ideally be of the same length for accurate comparison

Conclusion

Sequence-dependent array comparison is useful when element order matters. Use the collection method when you need the actual matching values, or the counting method when you only need the total number of matches.

Updated on: 2026-03-15T23:19:00+05:30

408 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements