Remove duplicate types from an unsorted array. This produces a potentially smaller array with all duplicates removed. Null is permitted in the array and will be preserved, though duplicates of it will not be. Items in the array are compared using Object.equals() . @param items
(T[] items)
| 287 | * @return |
| 288 | */ |
| 289 | public static <T> T[] removeDuplicates(T[] items) { |
| 290 | int count = 0; |
| 291 | // First, identify duplicates and store this information in a bitset. |
| 292 | BitSet duplicates = new BitSet(items.length); |
| 293 | for (int i = 0; i != items.length; ++i) { |
| 294 | T ith = items[i]; |
| 295 | for (int j = i + 1; j < items.length; ++j) { |
| 296 | T jth = items[j]; |
| 297 | if(ith == null) { |
| 298 | if(jth == null) { |
| 299 | duplicates.set(i); |
| 300 | count = count + 1; |
| 301 | break; |
| 302 | } |
| 303 | } else if (ith.equals(jth)) { |
| 304 | duplicates.set(i); |
| 305 | count = count + 1; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | // Second, eliminate duplicates (if any) |
| 311 | if (count == 0) { |
| 312 | // nothing actually needs to be removed |
| 313 | return items; |
| 314 | } else { |
| 315 | T[] nItems = Arrays.copyOf(items, items.length - count); |
| 316 | for (int i = 0, j = 0; i != items.length; ++i) { |
| 317 | if (duplicates.get(i)) { |
| 318 | // this is a duplicate, ignore |
| 319 | } else { |
| 320 | nItems[j++] = items[i]; |
| 321 | } |
| 322 | } |
| 323 | return nItems; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Remove duplicate types from an sorted array, thus any duplicates are |
no test coverage detected