# Check if two Integer are anagrams of each other

• Difficulty Level : Easy
• Last Updated : 17 Sep, 2022

Given two integers A and B, the task is to check whether the given numbers are anagrams of each other or not. Just like strings, a number is said to be an anagram of some other number if it can be made equal to the other number by just shuffling the digits in it.
Examples:

Input: A = 204, B = 240
Output: Yes

Input: A = 23, B = 959
Output: No

Approach: Create two arrays freqA[] and freqB[] where freqA[i] and freqB[i] will store the frequency of digit i in a and b respectively. Now traverse the frequency arrays and for any digit i if freqA[i] != freqB[i] then the numbers are not anagrams of each other else they are.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `const` `int` `TEN = 10;` `// Function to update the frequency array``// such that freq[i] stores the``// frequency of digit i in n``void` `updateFreq(``int` `n, ``int` `freq[])``{` `    ``// While there are digits``    ``// left to process``    ``while` `(n) {``        ``int` `digit = n % TEN;` `        ``// Update the frequency of``        ``// the current digit``        ``freq[digit]++;` `        ``// Remove the last digit``        ``n /= TEN;``    ``}``}` `// Function that returns true if a and b``// are anagarams of each other``bool` `areAnagrams(``int` `a, ``int` `b)``{` `    ``// To store the frequencies of``    ``// the digits in a and b``    ``int` `freqA[TEN] = { 0 };``    ``int` `freqB[TEN] = { 0 };` `    ``// Update the frequency of``    ``// the digits in a``    ``updateFreq(a, freqA);` `    ``// Update the frequency of``    ``// the digits in b``    ``updateFreq(b, freqB);` `    ``// Match the frequencies of``    ``// the common digits``    ``for` `(``int` `i = 0; i < TEN; i++) {` `        ``// If frequency differs for any digit``        ``// then the numbers are not``        ``// anagrams of each other``        ``if` `(freqA[i] != freqB[i])``            ``return` `false``;``    ``}` `    ``return` `true``;``}` `// Driver code``int` `main()``{``    ``int` `a = 240, b = 204;` `    ``if` `(areAnagrams(a, b))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ``static` `final` `int` `TEN = ``10``;``    ` `    ``// Function to update the frequency array``    ``// such that freq[i] stores the``    ``// frequency of digit i in n``    ``static` `void` `updateFreq(``int` `n, ``int` `[] freq)``    ``{``    ` `        ``// While there are digits``        ``// left to process``        ``while` `(n > ``0``)``        ``{``            ``int` `digit = n % TEN;``    ` `            ``// Update the frequency of``            ``// the current digit``            ``freq[digit]++;``    ` `            ``// Remove the last digit``            ``n /= TEN;``        ``}``    ``}``    ` `    ``// Function that returns true if a and b``    ``// are anagarams of each other``    ``static` `boolean` `areAnagrams(``int` `a, ``int` `b)``    ``{``    ` `        ``// To store the frequencies of``        ``// the digits in a and b``        ``int` `[] freqA = ``new` `int``[TEN];``        ``int` `[] freqB = ``new` `int``[TEN];``    ` `        ``// Update the frequency of``        ``// the digits in a``        ``updateFreq(a, freqA);``    ` `        ``// Update the frequency of``        ``// the digits in b``        ``updateFreq(b, freqB);``    ` `        ``// Match the frequencies of``        ``// the common digits``        ``for` `(``int` `i = ``0``; i < TEN; i++)``        ``{``    ` `            ``// If frequency differs for any digit``            ``// then the numbers are not``            ``// anagrams of each other``            ``if` `(freqA[i] != freqB[i])``                ``return` `false``;``        ``}``        ``return` `true``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `a = ``204``, b = ``240``;``    ` `        ``if` `(areAnagrams(a, b))``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}` `// This code is contributed by ihirtik``       `

## Python3

 `# Python3 implementation of the approach``TEN ``=` `10` `# Function to update the frequency array``# such that freq[i] stores the``# frequency of digit i in n``def` `updateFreq(n, freq) :` `    ``# While there are digits``    ``# left to process``    ``while` `(n) :``        ``digit ``=` `n ``%` `TEN` `        ``# Update the frequency of``        ``# the current digit``        ``freq[digit] ``+``=` `1` `        ``# Remove the last digit``        ``n ``/``/``=` `TEN` `# Function that returns true if a and b``# are anagarams of each other``def` `areAnagrams(a, b):` `    ``# To store the frequencies of``    ``# the digits in a and b``    ``freqA ``=` `[ ``0` `] ``*` `TEN``    ``freqB ``=` `[ ``0` `] ``*` `TEN` `    ``# Update the frequency of``    ``# the digits in a``    ``updateFreq(a, freqA)` `    ``# Update the frequency of``    ``# the digits in b``    ``updateFreq(b, freqB)` `    ``# Match the frequencies of``    ``# the common digits``    ``for` `i ``in` `range``(TEN):` `        ``# If frequency differs for any digit``        ``# then the numbers are not``        ``# anagrams of each other``        ``if` `(freqA[i] !``=` `freqB[i]):``            ``return` `False``            ` `    ``return` `True` `# Driver code``a ``=` `240``b ``=` `204` `if` `(areAnagrams(a, b)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)` `# This code is contributed by``# divyamohan123`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ``static` `int` `TEN = 10;``    ` `    ``// Function to update the frequency array``    ``// such that freq[i] stores the``    ``// frequency of digit i in n``    ``static` `void` `updateFreq(``int` `n, ``int` `[] freq)``    ``{``    ` `        ``// While there are digits``        ``// left to process``        ``while` `(n > 0)``        ``{``            ``int` `digit = n % TEN;``    ` `            ``// Update the frequency of``            ``// the current digit``            ``freq[digit]++;``    ` `            ``// Remove the last digit``            ``n /= TEN;``        ``}``    ``}``    ` `    ``// Function that returns true if a and b``    ``// are anagarams of each other``    ``static` `bool` `areAnagrams(``int` `a, ``int` `b)``    ``{``    ` `        ``// To store the frequencies of``        ``// the digits in a and b``        ``int` `[] freqA = ``new` `int``[TEN];``        ``int` `[] freqB = ``new` `int``[TEN];;``    ` `        ``// Update the frequency of``        ``// the digits in a``        ``updateFreq(a, freqA);``    ` `        ``// Update the frequency of``        ``// the digits in b``        ``updateFreq(b, freqB);``    ` `        ``// Match the frequencies of``        ``// the common digits``        ``for` `(``int` `i = 0; i < TEN; i++)``        ``{``    ` `            ``// If frequency differs for any digit``            ``// then the numbers are not``            ``// anagrams of each other``            ``if` `(freqA[i] != freqB[i])``                ``return` `false``;``        ``}``        ``return` `true``;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `a = 204, b = 240;``    ` `        ``if` `(areAnagrams(a, b))``            ``Console.WriteLine(``"Yes"``);``        ``else``            ``Console.WriteLine(``"No"``);``    ``}``}` `// This code is contributed by ihirtik`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(log10a+log10b)

Auxiliary Space: O(1) as constant space is required.

#### Method #2 :Using sorting():

• Convert two integers to string.
• Sort the strings and check if they are equal.
• Print Yes if they are equal.
• Else no.

Below is the implementation:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that returns true if a and b``// are anagarams of each other``bool` `areAnagrams(``int` `a, ``int` `b)``{``  ` `    ``// Converting numbers to strings``    ``string c = to_string(a);``    ``string d = to_string(b);` `    ``// Checking if the sorting values``    ``// of two strings are equal``    ``sort(c.begin(), c.end());``    ``sort(d.begin(), d.end());``    ``if` `(c == d)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Driver code``int` `main()``{``    ``int` `a = 240;``    ``int` `b = 204;` `    ``if` `(areAnagrams(a, b))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;``}` `// This code is contributed by ukasp.`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `GFG {` `    ``// Function that returns true if a and b``    ``// are anagarams of each other``    ``static` `boolean` `areAnagrams(``int` `a, ``int` `b)``    ``{` `        ``// Converting numbers to strings``        ``char``[] c = (String.valueOf(a)).toCharArray();``        ``char``[] d = (String.valueOf(b)).toCharArray();` `        ``// Checking if the sorting values``        ``// of two strings are equal``        ``Arrays.sort(c);``        ``Arrays.sort(d);` `        ``return` `(Arrays.equals(c, d));``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `a = ``240``;``        ``int` `b = ``204``;` `        ``System.out.println((areAnagrams(a, b)) ? ``"Yes"``                                               ``: ``"No"``);``    ``}``}` `// This code is contributed by phasing17.`

## Python3

 `# Python3 implementation of the approach``# Function that returns true if a and b``# are anagarams of each other``def` `areAnagrams(a, b):``  ` `    ``# Converting numbers to strings``    ``a ``=` `str``(a)``    ``b ``=` `str``(b)``    ` `    ``# Checking if the sorting values``    ``# of two strings are equal``    ``if``(``sorted``(a) ``=``=` `sorted``(b)):``        ``return` `True``    ``else``:``        ``return` `False`  `# Driver code``a ``=` `240``b ``=` `204` `if` `(areAnagrams(a, b)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)` `# This code is contributed by vikkycirus`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Linq;``using` `System.Collections.Generic;` `class` `GFG``{``  ` `    ``// Function that returns true if a and b``    ``// are anagarams of each other``    ``static` `bool` `areAnagrams(``int` `a, ``int` `b)``    ``{` `        ``// Converting numbers to strings``        ``char``[] c = (Convert.ToString(a)).ToCharArray();``        ``char``[] d = (Convert.ToString(b)).ToCharArray();` `        ``// Checking if the sorting values``        ``// of two strings are equal``        ``Array.Sort(c);``        ``Array.Sort(d);``        ``return` `(c.SequenceEqual(d));``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int` `a = 240;``        ``int` `b = 204;` `        ``Console.WriteLine((areAnagrams(a, b)) ? ``"Yes"``                                              ``: ``"No"``);``    ``}``}` `// This code is contributed by phasing17.`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(d1*log(d1)+d2*log(d2)) where d1=log10a and d2=log10b

Auxiliary Space: O(d1+d2)

My Personal Notes arrow_drop_up