Monday, May 11, 2020

Socks problem - Can you help me with better logic ?


Problem:

Solution:
static int sockMerchant(int n, int[] ar) {

        int cnt=0;
        Arrays.sort(ar);
        for(int i=0;i<n-1;i++)
            if(ar[i]==ar[i+1]){cnt++;i++;}
        return cnt;

    }