Sort by

recency

|

5995 Discussions

|

  • + 0 comments

    **The Best Solution Without Object OverHead from Hash DS. **

    import java.util.Scanner;

    public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt();

        BitSet bset = new BitSet(100);
        int count = 0;
    
        for(int i = 0; i < n; i++) {
            int num = scan.nextInt();
            //if toggled on
            if(bset.get(num)) {
                count++;
                bset.clear(num);
            }else {
                bset.set(num);
            }
        }
    
        System.out.println(count);
        scan.close();
    }
    

    }

    class BitSet{ private long[] bits; private int size;

    public BitSet(int size) {
        this.size = size;
        this.bits = new long[(size + 64) / 64];
    }
    
    public BitSet() {
        this(64);
    }
    
    public void set(int index) {
        if(index < 0) {
            throw new IndexOutOfBoundsException("index must be > 0");
        }else if(index > size) {
            resize(index);
        }
    
        bits[index / 64] |= (1L << (index % 64));
    }
    
    public boolean get(int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index must be > 0 & < size");
        }
    
        return (bits[index / 64] & (1L << (index % 64))) != 0;
    }
    
    public void clear(int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("Index must be > 0 & < size");
        }
    
        bits[index / 64] &= ~(1L << (index % 64));
    }
    
    public int size() {
        return size;
    }
    
    private void resize(int index) {
        long[] newBits = new long[(index + 64) / 64];
        System.arraycopy(bits, 0, newBits, 0, bits.length);
        bits = newBits;
        size = index;
    }
    

    }

  • + 0 comments
       public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
        int count = 0;
        for(int i=0; i<ar.size(); i++) {
            for(int j=i+1; j<ar.size(); j++) {
                if(ar.get(j)==-1) {
                    continue;
                }
                if(ar.get(i)==ar.get(j)) {
                    count++;
                    ar.set(j,-1);
                    break;
                }
            }
        }
        return count;
    
        }
    
    }
    
  • + 0 comments

    Solution C#:

        public static int sockMerchant(int n, List<int> ar)
                {
                        int pairCount = 0; 
                        var sockDict = new Dictionary<int , int>();
                        foreach(var item in ar){
                                if(sockDict.ContainsKey(item)){
                                        sockDict[item] += 1;
                                }
                                else {
                                        sockDict[item] = 1;
                                }
                        }
    
                        foreach(var kvp in sockDict){
                                pairCount+= (int)kvp.Value/2;
                        }
    
                        return pairCount;
                }
    
  • + 0 comments
        public static int sockMerchant(int n, List<Integer> ar) {
        // Write your code here
            Map<Integer, Integer> map = new HashMap<>();
                
            ar.stream().forEach(val -> {
                if (map.containsKey(val)) {
                    map.remove(val);
                } else {
                    map.put(val, 1);
                }
            });
            
            return (ar.size() - map.keySet().size()) / 2;
        }
    
  • + 0 comments
    // Javascript
    function sockMerchant(n, ar) {
        let pairCount = 0;
        let i = 0;
        ar.sort((a,b) => a - b);
        while(i < n - 1){
            if(ar[i] == ar[i+1]){
                pairCount++;
                i+=2;
            }
            else {
                i++;
            }
        }
        return pairCount;
    }