Frequently Asked Java Program 16: How To Check The Equality Of Two Arrays

preview_player
Показать описание
Topic : How To Check The Equality Of Two Arrays

#########################
Udemy Courses:
#########################

Manual Testing+Agile with Jira Tool
************************************

Selenium with Java+Cucumber
********************************

Selenium with Python & PyTest
********************************

Selenium with python using Robot framework
****************************************

API Testing(Postman, RestAssured & SoapUI)
*****************************************

Web & API Automation using Cypress with Javascript
********************************************

Playwright with Javascript
**************************

Jmeter-Performance Testing
************************

SDET Essencials(Full Stack QA)
*************************

Appium-Mobile Automation Testing
************************************

Java Collections
*****************

Python Programming
*********************

Cucumber BDD Framework
***************************

Protractor with Javascript
***************************

####################################
Youtube Playlists:
####################################

Manual Testing & Agile
***********************

SQL
*************************

linux & Shell Scripting
**********************

Java
**********************

Selenium With Java+Cucumber
********************************

Python
********************************

Selenium With Python,Pytest&Behave
***************************************

Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************

API Testing (Postman,SoapUi,&Rest Assured)
**********************************************

Mobile App Testing Appium
****************************

Performance Testing Jmeter
*******************************

Maven,Jenkins,Git,Github,CI/CD
*******************************

SQL,DB Testing&ETL,Bigdata
*******************************

JavaScript Based Automation Tools
********************************

Selector Hub Tools
********************

GraphQL
******************

Cypress API Testing
********************

Cypress Web Testing
**********************

Playwright with Javascipt
**************************

#PythonArrayEquality
#ArrayComparison
#ArrayEqualityCheck
#PythonArrays
#ArrayMatching
#CompareArrays
#ArrayEqualityProgram
#PythonCoding
#ArrayOperations
#CodeLogic
#PythonProgramming
#CodingChallenge
#ArrayLogic
#ProgrammingPractice
#EqualityCheck
#AlgorithmDesign
#PythonSolutions
#ArrayProblems
#CodingSkills
#PythonSnippets
#ArrayPuzzles
#AlgorithmAnalysis
#ProblemSolving
#ArrayAlgorithms
#PythonCodingTips
#ArrayComparisonLogic
#CodeOptimization
#PythonLearning
#ArrayPatterns
#AlgorithmApproach
#CodingCommunity
#PythonHelp
#ArrayTricks
#ProblemSolvingInPython
#AlgorithmTricks
#CodingInPython
#ArrayManipulation
#PythonTricks
#AlgorithmExplained
#CodeBreakdown
#PythonArrays101
#AlgorithmMastery
#CodingInspiration
#PythonExploration
#ArrayTips
#AlgorithmHacks
#CodingGenius
#PythonCodeSnippets
#ArrayComparisonTricks
#AlgorithmSolutions
Рекомендации по теме
Комментарии
Автор

Another approach:
int[] arr01 = {2, 3, 5, 7, 11, 13, 17, 19, 23};
int[] arr02 = {7, 3, 5, 2, 11, 13, 17, 19, 23};
if (arr01.length == arr02.length) {
int counter = 0;
for (int element : arr01) {
for (int element2 : arr02) {
if (element == element2) {
counter++;
break;
}
}
}
if (counter == arr01.length) {
System.out.println("The two arrays are equal");
} else {
System.out.println("The two arrays are not equal");
}
} else {
System.out.println("The two arrays are not equal");
}

HocineFerradj
Автор

I like your tutorials. thank you very much!!

addisutigabu
Автор

sir, i think this is very easy logic, ,, just once check plzz...
//WAP to check if 2 arrays are equal or not using HashSet:
import java.util.HashSet;
class Check2ArraysEqual1
{
public static void main(String[] args)
{
int[] arr1={1, 2, 3, 4, 5};
int[] arr2={1, 6, 2, 4, 5};
HashSet<Integer> hs1=new HashSet<>();
HashSet<Integer> hs2=new HashSet<>();
for(int i=0;i<arr1.length;i++)
{
hs1.add(arr1[i]);
hs2.add(arr2[i]);
}
if(hs1.equals(hs2))
{
System.out.print("2 arrays are equal");
}
else
{
System.out.print("2 arrays are not equal");
}
}
}

Aliya
Автор

if(a1[i] != a2[i]){
status = false;
break; // very important !!
}

HocineFerradj
Автор

public class HowToCheckArrayisequal {

public static void main(String args[]) {
int a [] = {1, 2, 3, 4, 5, 6, 7};
int a1 [] = {1, 2, 3, 4, 5, 6, 7, 5};

boolean flag = true;

if(a.length==a1.length) {
for(int i =0;i<a.length;i++) {
if(a[i] !=a1[i]) {
flag = false;
}
}
}
else
{
flag = false ;
}

? "Array is equal ":"array is not equal"));
}

}

motivationmotivational
Автор

This logic shall work only when last value that is a[4] is not matching we get false, but if a1[0 to 3] not equal to a2[0 to 3] and a1[4]=a2[4] this logic should not work.

sudhakargunta
Автор

What are the pros and cons of using each strategy?

jntula
Автор

public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 4, 5};

int len=a.length;
int count=0;

if(a.length==b.length)
{
for(int i=0;i<a.length;i++)
{
if(a[i]==b[i])
{

}
else
{
arrays are not equals");
}
}

}
else
{
System.out.println("given arrays are not equals");
}
if(count==len)
{
System.out.println("Given Arrays are same");
}
}

chandrakantmane
Автор

public static void main(String[] args) {
int[] a1 = { 1, 2, 3, 4 };
int[] a2 = { 1, 1, 1, 4 };

int found = 0;
if (a1.length == a2.length) {
for (int i = 0; i < a1.length; i++) {

int no = a1[i];

for (int j = 0; j < a2.length; j++) {

(no == a2[j]) {



}

if (found > 1 || found == 0) {
matching found.11.F:"+found);

}

found = 0;

}
} else {
System.out.println("no match found.22...");
}

mbs
Автор

Your program is weird, This is easy example :-

int[] arr = {1, 2, 3, 6, 10, 9};
int[] arr2 = {1, 22, 3, 6, 10, 9};
boolean isArrayEqual = true;

for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr2.length; j++) {
if (arr[i] == arr2[j]) {
break;
} else {
isArrayEqual = false;
}
}
}

if (isArrayEqual) {
System.out.println("EQUAL ARRAY");
} else {
System.out.println("NOT EQUAL ARRAY");
}

heavenlife