---
title: "Write a Java program to check if two arrays contain the same elements."  
description: "Write a Java program to check if two arrays contain the same elements."  
author: "Ashutosh Patel"  
published: 2023-03-22  
updated: 2023-03-22  
canonical: https://www.mindstick.com/forum/157544/write-a-java-program-to-check-if-two-arrays-contain-the-same-elements  
category: "java"  
tags: ["java", "programs"]  
reading_time: 1 minute  

---

# Write a Java program to check if two arrays contain the same elements.

Write a [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [program to check](https://www.mindstick.com/forum/157542/write-a-java-program-to-check-if-a-list-of-integers-contains-only-odd-numbers) if [two arrays](https://www.mindstick.com/forum/161074/how-to-compare-two-arrays-to-each-other-in-javascript) contain the same elements.

## Replies

### Reply by Krishnapriya Rajeev

The code given below checks [if two](https://answers.mindstick.com/qa/35072/how-to-check-if-two-string-are-anagram) integer [arrays](https://www.mindstick.com/articles/11955/arrays-and-its-limitations) are the same or not.

```plaintext
public class equal {
    public static void main(String[] args)
    {
        // Initialize the first array
        int a[] = { 10, 30, 11 };
        // Initialize the second array
        int b[] = { 10, 30, 12 };

        int flag = 1;
        // Check if the elements are same or not
        if (a.length == b.length) {

            for (int i = 0; i < a.length; i = i + 1) {

					if (a[i] != b[i]) {
                    flag=0;
                    break; //if the elements are different, set flag as 0 and break out of the loop
                }
            }
        }
        else {
            flag = 0;
        }

        if (flag==1) {
            System.out.println("Arrays are equal");
        }
        else {
            System.out.println("Arrays are not equal");
        }
    }
}

OUTPUT:
Arrays are not equal
```


---

Original Source: https://www.mindstick.com/forum/157544/write-a-java-program-to-check-if-two-arrays-contain-the-same-elements

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
