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

  • Home
  • SEARCH
  • 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 8408751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:43:10+00:00 2026-06-09T23:43:10+00:00

Given two integer arrays of size N, design an algorithm to determine whether one

  • 0

Given two integer arrays of size N, design an algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order.

I can think of two ways:
Sort them and compare : O(N.log N + N)

Check if the array have same number of integers and the sum of these integers is same, then XOR both the arrays and see if the result is 0. This is O(N). I am not sure if this method will eliminate false positives completely. Thoughts. Better algorithms?

  • 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-06-09T23:43:11+00:00Added an answer on June 9, 2026 at 11:43 pm

    Check if the array have same number of integers and the sum of these integers is same, then XOR both the arrays and see if the result is 0.

    This doesn’t work. Example:

    a = [1,6]  length(a) = 2, sum(a) = 7, xor(a) = 7
    b = [3,4]  length(b) = 2, sum(b) = 7, xor(b) = 7
    

    Others have already suggested HashMap for an O(n) solution.

    Here’s an O(n) solution in C# using a Dictionary<T, int>:

    bool IsPermutation<T>(IList<T> values1, IList<T> values2)
    {
        if (values1.Count != values2.Count)
        {
            return false;
        }
    
        Dictionary<T, int> counts = new Dictionary<T, int>();
        foreach (T t in values1)
        {
            int count;
            counts.TryGetValue(t, out count);
            counts[t] = count + 1;
        }
    
        foreach (T t in values2)
        {
            int count;
            if (!counts.TryGetValue(t, out count) || count == 0)
            {
                return false;
            }
            counts[t] = count - 1;
        }
    
        return true;
    }
    

    In Python you could use the Counter class:

    >>> a = [1, 4, 9, 4, 6]
    >>> b = [4, 6, 1, 4, 9]
    >>> c = [4, 1, 9, 1, 6]
    >>> d = [1, 4, 6, 9, 4]
    >>> from collections import Counter
    >>> Counter(a) == Counter(b)
    True
    >>> Counter(c) == Counter(d)
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two sorted arrays of integers, a and b , and an integer c
You are given two sorted integer arrays, which have some common integers. Any common
I have two integer arrays which contain numeric values. I want to look through
Given two arrays, how to find the maximum element which is common to both
For my algorithm design class homework came this brain teaser: Given a list of
This program so far has one purpose, take in two integers(the size of the
Need Hints to design an efficient algorithm that takes the following input and spits
Possible Duplicate: Given two arrays a and b .Find all pairs of elements (a1,b1)
The problem is easy to explain: we have two big arrays (32 bit integer
Given two integer numbers N and n (N >= n > 0), how do

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.