java - Given an unordered stream of integers maintain the ordering but remove the elements that occur exactly once in the array -
my solution: public class removeuniqueelements { public static integer[] removeunique(int[] arr){ map<integer,integer> output = new hashmap<integer,integer>(); list<integer> result = new arraylist<integer>(); for(int i=0;i<arr.length;i++){ if(output.containskey(arr[i])){ int count = output.get(arr[i]); count = count+1; output.put(arr[i],count); } else { output.put(arr[i],1); } } for(int i=0;i<arr.length;i++){ if(output.get(arr[i])!=1){ result.add(arr[i]); } } return result.toarray(new integer[result.size()]); } public static void main(string[] args){ int[] array = {1,2,3,4,2,3,4}; integer[] result = removeunique(array); system.out.println(arrays.tostring(result)); } } time complexity : o(n) s