CapitalIQ Interview Question
ConsultantsCountry: India
Interview Type: Phone Interview
Just sort the array and then start traversing the array and keep the traversed element in another array..if during traversing you get a condition where arr[i]==arr[i+1] then ignore this value otherwise keep copying the elements.It will time for sorting the array and then traversing which will take O(nlogn).
1. consider given array(arr ), other array (Oarr), and a hash Map (HMap<int,int>);
2. key of HMap is element of arr,
3. if ith element of arr has count>0 in HMap, ignore it otherwise store in Oarr.
map mymap;
int givenarr[n];
int otherarr[n];
j = 0
for i = 0 to n - 1
if not mymap.containskey(givenarr[i])
otherarr[j++] = givenarr[i]
mymap[givenarr[i]] = something
while j < n
otherarr[j++] = Nothing
O(nlogn)
1. sort the array in ascending order( O(log n) )
2. and simply copy element from given array to another array and keep record of last copied in element in variable. continue this process till the end of array. (O(n))
so total time complexity O(n).
Since a hashset maintains uniqueness of elements, why not just add the elements from the array to a hashset, iterate over the hashset and print them? Time complexity: O(n).
- Murali Mohan June 19, 2013