Android Array Remove deleteSubset(int[] multiset, int[] subset)

Here you can find the source of deleteSubset(int[] multiset, int[] subset)

Description

Iterate over the elements in the subset and set the first elements to -1 in the multiset which equals the subset elements.

License

Open Source License

Parameter

Parameter Description
multiset a multiset containing equals or greater than elements as the subset.
subset a subset with the elements which should be deleted.

Declaration

public static int[] deleteSubset(int[] multiset, int[] subset) 

Method Source Code

//package com.java2s;
/**/*from  w w w  .j ava 2 s .co  m*/
 * jcombinatorics:
 * Java Combinatorics Library
 *
 * Copyright (c) 2009 by Alistair A. Israel.
 *
 * This software is made available under the terms of the MIT License.
 * See LICENSE.txt.
 *
 * Created Aug 31, 2009
 */

public class Main {
    /**
     * Iterate over the elements in the <code>subset</code> and set the first
     * elements to <code>-1</code> in the <code>multiset</code> which equals the
     * subset elements. Return a new reduced multiset <code>int[]</code> array
     * where the <code>-1</code> values are deleted.
     * 
     * @param multiset
     *            a multiset containing equals or greater than elements as the
     *            subset.
     * @param subset
     *            a subset with the elements which should be deleted.
     * @return
     */
    public static int[] deleteSubset(int[] multiset, int[] subset) {
        int size = multiset.length;
        int[] setClone = new int[size];// multiset.clone();
        System.arraycopy(multiset, 0, setClone, 0, size);
        int k = 0;
        for (int j = 0; j < subset.length; j++) {
            for (int i = k; i < setClone.length; i++) {
                if (subset[j] == setClone[i]) {
                    setClone[i] = -1;
                    size--;
                    k = i + 1;
                    break;
                }
            }
        }

        int[] result = new int[size];
        k = 0;
        for (int i = 0; i < setClone.length; i++) {
            if (setClone[i] != -1) {
                result[k++] = setClone[i];
            }
        }
        return result;
    }
}

Related

  1. remove(Object array, int index)
  2. remove(T[] array, int index)
  3. remove(boolean[] array, int index)
  4. remove(byte[] array, int index)