
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Checking the equality of array elements (sequence dependent) in JavaScript
In this article, the given task is to check the equality of array elements (sequence dependent). Before we proceed into examples, let’s look at how the input-output scenario will look when we are checking the equality of array elements (sequence dependent) in JavaScript.
Input-Output scenario
Let’s look into the input-output scenario, where we have declared two arrays and we need to get similar elements in sequence-dependent.
Array1 = [2, 5, 6, 7, 9, 0]; Array2 = [3, 5, 0, 8, 9, 4]; Output = [5, 9]
In the above snippet, we can see that there are two arrays with elements in them. we found that 5 and 9 are the same sequence in both arrays.
Example 1
In the following example below,
We have declared two arrays with elements in them. By iterating the first array (array1) we are comparing every element in the first array with elements in the second array.
Whenever there is a match in the same sequence order, that element will be pushed into an empty array.
<!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>
As we can see in the output, 10, 44, and 70 are matching in both the arrays and they are in the same sequence. So, we pushed those elements into an empty array.
Example 2
In the following example below,
We have declared two arrays with elements in them. whenever the elements of both arrays are matching in the same sequence order, the count will be incrementing which is initially declared as 0.
<!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>
In the output, the count got incremented whenever there is a match in both the arrays in the same sequence order.
- Related Articles
- Compare array elements to equality - JavaScript
- Equality of corresponding elements in JavaScript
- JavaScript Checking if all the elements are same in an array
- Checking dependent object of a table in SAP HANA
- Checking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript
- Checking progressive array - JavaScript
- Checking if two arrays can form a sequence - JavaScript
- Checking the intensity of shuffle of an array - JavaScript
- Removing least number of elements to convert array into increasing sequence using JavaScript
- Strict equality vs Loose equality in JavaScript.
- Checking digit sum of smallest number in the array in JavaScript
- Checking for vowels in array of numbers using JavaScript
- JavaScript Array: Checking for multiple values
- Checking an array for palindromes - JavaScript Â
- Checking for uniqueness in an array in JavaScript
