There is one question which is bugging me, and somehow, I cannot figure out what to do with it. Suppose an array {9,1,2,4,1,2,2}
is given. The unique elements in the array are 9
and 4
. The output array should be {1,2,1,2,2}
.
My idea to preserve the order and find duplicates is to use a LinkedHashMap which will have the entries and the count of occurrence of the entries.
The problem is maintaining the order of the elements. Once I put the entries in the hashMap, the order will vanish.