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

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

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.

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