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

• Array include non sorted, negative or positive integers.

• 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.

• 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}``````

• ``````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}
``````

• 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.

• ``````   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;
}``````

• ``````>>> 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]
``````

• ``````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;
},[]);
}
``````

• @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.

• @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

• ``````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``````

• 1.sort array
2.compare element j and element i until they are different. if the distance is 1, then element j is unique.

List<Integer> getNonDuplicate(int[] nums) {
Arrays.sort(nums);
List<Integer> res = new ArrayList<>();

``````    int len = nums.length;
int j = 0;
int i = 0;
while (++i < len) {
if (i < len && nums[j] == nums[i]) {
continue;
}

if (i == j + 1) {
res.add(nums[j]);
}
j = i;
}

if (i == j+1) {
res.add(nums[j]);
}

return res;
}``````

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