Easy solution Java
\n# Approach\ncount number of unequal pairs and reject if unequal pairs are more than 4 otherwise handle separately for 4,3 or 2 pairs.\n\n# Complexity\n- Time 
\n# Approach\ncount number of unequal pairs and reject if unequal pairs are more than 4 otherwise handle separately for 4,3 or 2 pairs.\n\n# Complexity\n- Time complexity:\n  $$O(n^2)$$\n\n- Space complexity:\n  $$O(1)$$ \n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        int cnt=0;\n        for(int i=0; i<nums.length-1; i++) {\n            for(int j=i+1; j<nums.length; j++) {\n                int x=nums[i];\n                int y = nums[j];\n                if(x==y) {\n                    cnt++;\n                    continue;\n                }\n\n                int unequalsDigits=0;\n                // for at most 2 swaps, at most 4 unequalDigit pairs are possible,\n                // more than 4 will result in numbers inelegible to count \n                int[][] d = {\n                    {-1, -1},\n                    {-1, -1},\n                    {-1, -1},\n                    {-1, -1},\n                };\n\n                while(x!=0 || y!=0) {\n                    //remainders  ( digits ) to compare\n                    int r1 = x%10;\n                    int r2 = y%10;\n\n                    x/=10;\n                    y/=10;\n                    \n                    if(r1==r2) continue;\n                    else unequalsDigits++;\n\n\n                    if(unequalsDigits==1) {\n                        d[0][0]=r1;\n                        d[0][1]=r2;\n                    }\n                    else if(unequalsDigits==2) {\n                        d[1][0]=r1;\n                        d[1][1]=r2;\n                        \n                    }\n                    else if(unequalsDigits==3) {\n                        d[2][0]=r1;\n                        d[2][1]=r2;\n                        \n                    }\n                    else if(unequalsDigits==4) {\n                        d[3][0]=r1;\n                        d[3][1]=r2;\n                        \n                    }else if(unequalsDigits>4) {\n                        break;\n                    }\n\n                }\n\n                if(unequalsDigits>4) continue;\n\n                if(unequalsDigits==4) {\n                    // make pairs of 2 ,2 for the swap to happen\n                    HashMap<Integer, Integer> map = new HashMap<>();\n\n                    for(int rw1=0; rw1<3; rw1++) {\n                        if(map.containsKey(rw1)) continue;\n                        for(int rw2=rw1+1; rw2<4; rw2++) {\n                            if(map.containsKey(rw2)) continue;\n                            int[] a = d[rw1];\n                            int[] b = d[rw2];\n                            if(a[0]==b[1] && a[1]==b[0]) {\n                                map.put(rw1, rw2);\n                                map.put(rw2, rw1);\n                                break;\n                            }\n                        }\n                    }\n\n                    if(map.size()==4) cnt++;\n                }else if(unequalsDigits==3) {\n\n                    // for 2 swaps the numbers will have to interchange with one another twice\n                    // eg. 3,2,1\n                    //     1,3,2   swap 1 and 2 first in first row than swap 1 and 3 \n                    // so for digits of first row we increment frequency of digits\n                    // and for digts of second row we decrease \n                    // tha check if the numbers are not adjusted back to zero meaning different digits are present and cannot be made equals after swaps.\n                    int[] fq = new int[10];\n                    fq[d[0][0]]++;\n                    fq[d[1][0]]++;\n                    fq[d[2][0]]++;\n                    fq[d[0][1]]--;\n                    fq[d[1][1]]--;\n                    fq[d[2][1]]--;\n                    \n                    for(int t=0; t<10; t++) {\n                        if(fq[t]!=0) {\n                            cnt--;\n                            break;\n                        }\n                    }\n                    cnt++;\n                }\n                else if(unequalsDigits==2) {\n                    // similer to Q 2, just check if the unequalsDigit pairs are swappable to make numbers equal.\n                    if(d[0][0]==d[1][1] && d[1][0]==d[0][1]) cnt++;\n                }\n            }\n        }\n\n        return cnt;\n    }\n}\n```
['Java']