java - How to efficiently remove duplicates from an array without using Set -
i asked write own implementation remove duplicated values in array. here have created. after tests 1,000,000 elements took long time finish. there can improve algorithm or bugs remove ?
i need write own implementation - not use set
, hashset
etc. or other tools such iterators. array remove duplicates.
public static int[] removeduplicates(int[] arr) { int end = arr.length; (int = 0; < end; i++) { (int j = + 1; j < end; j++) { if (arr[i] == arr[j]) { int shiftleft = j; (int k = j+1; k < end; k++, shiftleft++) { arr[shiftleft] = arr[k]; } end--; j--; } } } int[] whitelist = new int[end]; for(int = 0; < end; i++){ whitelist[i] = arr[i]; } return whitelist; }
you can take of set collection
int end = arr.length; set<integer> set = new hashset<integer>(); for(int = 0; < end; i++){ set.add(arr[i]); }
now if iterate through set, contain unique values. iterating code :
iterator = set.iterator(); while(it.hasnext()) { system.out.println(it.next()); }
Comments
Post a Comment