Return a list of non-duplicate integers a given array, i.e., given [1,2,3,4,4,3], return [1,2]


  • 0
    N

    Array include non sorted, negative or positive integers.


  • 0

    A simple solution is to use a hash map <Integer, Boolean> to track if an integer has duplicate or not, then go through the input array second time and put non duplicate integer to the final result, but maybe there is a better solution.


  • 0
    S

    This is a python function to find the non-duplicate items:

    def non_duplicates(items):
        mp = {}
        for i in items:
            mp[i] = mp[i] + 1 if i in mp else 1
        return {i for i in mp.keys() if mp[i] == 1}
    

    Test:

    print non_duplicates([1, 5, 4, 3, 2, 6, 5, 7, 4, 2, 1, 9, 8,])
    
    {3, 6, 7, 8, 9}

  • -1
    R
    Simple way of finding non-duplicates using Python :
        def non_duplicates(items):
            mp = []
            for i in items:
                if not(i in mp): mp.append(i)
                else : mp.remove(i)
            return mp
    
    To Test:
    
    print(non_duplicates([1, 5, 4, 3, 2, 6, 5, 7, 4, 2, 1, 9, 8,]))
    
    {3, 6, 7, 8, 9}
    

  • 1
    D

    As soon as you encounter the word duplicate in a set. Your first thinking should be using HashSet or HashTable. Yes. HashSet is a good data structure for this problem to solve.


  • 0
    S
       public static int[] removeDuplicate(int[] arr) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            int sizeOfDistinct = 0;
            for(int i=0;i<arr.length;i++) {
                if(map.containsKey(arr[i])) {
                    map.put(arr[i], map.get(arr[i])+1);
                    sizeOfDistinct--;
                } else {
                    map.put(arr[i], 1);
                    sizeOfDistinct++;
                }
            }
    
            int[] res = new int[sizeOfDistinct];
            int i=0;
            
            for(Integer cur: map.keySet()) {
                if(map.get(cur) == 1) {
                    res[i++] = cur;
                }
            }
    
            return res;
        }

  • 0
    M
    >>> nums = [1,2,3,4,4,3]
    >>> def getUnique(nums):
    ...   result, int_set = set(), set()
    ...   for num in nums:
    ...     result.add(num) if num not in int_set else result.remove(num)
    ...     int_set.add(num)
    ...   return list(result)
    ...
    >>> getUnique(nums)
    [1, 2]
    

  • 0
    N
    function nonDuplicate(array){
      var map = {};
      for (var i = 0, length = array.length; i < length; i++) {
        var value = array[i];
        if (value in map) {
          map[value] = true;
        } else {
          map[value] = false;
        }
      }
    
      return Object.keys(map).reduce(function (acc, current) {
        if (!map[current]) {
          acc.push(parseInt(current));
        }
        return acc;
      },[]);
    }
    

  • 0
    E

    @rajashree_g you're using a list so each operation will take O(n) thus making your implementation O(n^2). Also your implementation will fail when a duplicate element exists an odd number of times as you're simply adding if the element doesn't exist and removing if the element exists.


  • 0
    F

    @rajashree_g

    I think this might have a problem if the item is in there 3 times? It got removed on 2nd but added back in the 3rd


  • 0
    S
    def uniq(nums):
       totals = set()
       uniqs = set()
       for num in nums:
          if num not in totals:
             uniqs.add(num)
          else:
              uniqs.remove(num)
          totals.add(num)
       return uniqs

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.