Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:29+00:00 2026-05-26T18:13:29+00:00

I wrote below code to compare to arrays that have same elements but in

  • 0

I wrote below code to compare to arrays that have same elements but in diff order.

 Integer arr1[] = {1,4,6,7,2};
 Integer arr2[] = {1,2,7,4,6};

For example, Above arrays are equal as they same elements 1,2,4,6,7. If you have better code for larger arrays, please share.

Edit If unique elements are taken from both the arrays and if they appear to be same, then also array should be equal. How do I write the code without using any collections classes.
Ex: arr1={1,2,3,1,2,3} arr2={3,2,1} Method should return true (=both arrays are same).

package com.test;

public class ArrayCompare {

public boolean compareArrays(Integer[] arr1, Integer[] arr2){
    if(arr1==null || arr2==null){
        return false;
    }
    if(arr1.length!=arr2.length){
        return false;
    }

    Integer[] sortedArr1=sortArray(arr1);
    Integer[] sortedArr2=sortArray(arr2);

    for(int i=0;i<sortedArr1.length-1;i++){
        if(sortedArr1[i]!=sortedArr2[i]){
            return false;
        }
    }
     return true;
}
public void swapElements(Integer[] arr,int pos){
    int temp=arr[pos];
    arr[pos]=arr[pos+1];
    arr[pos+1]=temp;
}
public Integer[] sortArray(Integer[] arr){
    for(int k=0;k<arr.length;k++){
        for(int i=0;i<arr.length-1;i++){
            if(arr[i]>arr[i+1]){
                swapElements(arr,i);
            }
        }
    }
    return arr;
}


public static void main(String[] args) {
    Integer arr1[] = {1,4,6,7,2};
    Integer arr2[] = {1,2,7,4,6};
    ArrayCompare arrComp=new ArrayCompare();
    System.out.println(arrComp.compareArrays(arr1, arr2));
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:13:29+00:00Added an answer on May 26, 2026 at 6:13 pm

    Do you care about duplicate counts? For example, would you need to distinguish between { 1, 1, 2 } and { 1, 2, 2 }? If not, just use a HashSet:

    public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
        HashSet<Integer> set1 = new HashSet<Integer>(Arrays.asList(arr1));
        HashSet<Integer> set2 = new HashSet<Integer>(Arrays.asList(arr2));
        return set1.equals(set2);
    }
    

    If you do care about duplicates, then either you could use a Multiset from Guava.

    If you want to stick with the sorting version, why not use the built-in sorting algorithms instead of writing your own?

    EDIT: You don’t even need to create a copy, if you’re happy modifying the existing arrays. For example:

    public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }
    

    You can also have an optimization for the case where the arrays aren’t the same length:

    public static boolean compareArrays(Integer[] arr1, Integer[] arr2) {
        // TODO: Null validation...
        if (arr1.length != arr2.length) {
            return false;
        }
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
I wrote below code to create an extra button on Calculator, but the button
I wrote code below that is very similar to the accepted answer here .
I wrote a custom ordering LINQ extension method as below but I think it
I don't have access to my dev environment, but when I write the below:
I'm using the code below to compare the last line of a log minus
it's kind a simple question but i have the doubt see the code belows
I wrote the below code for getting lat long positions myLocationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE);
I wrote the below code which replaces '|' characters from the string. #include <stdio.h>
I write the code below to delete a file: FileInfo file = new FileInfo(filename);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.