MCPcopy Create free account
hub / github.com/Whiley/WhileyCompiler / removeDuplicates

Method removeDuplicates

src/main/java/wycc/util/ArrayUtils.java:289–325  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 5

createMethod · 0.95
leastUpperBoundMethod · 0.95
SubtypeConstraintsMethod · 0.95
intersectMethod · 0.95
toStringMethod · 0.95

Calls 3

setMethod · 0.65
getMethod · 0.65
equalsMethod · 0.45

Tested by

no test coverage detected