how to remove values in one array from another

Does anyone know how to remove arrays in one array from another, without using ArrayList?

As an example:

int[] set01 = {1, 2, 3, 4, 5, 6};
int[] set02 = {1, 2};
int[] set03;

//function to remove set02 from set01, yielding set03 = {3, 4, 5, 6}

Answers

  • edited May 2018

    You could write your own function to do it.

    Look at each item in set01.

    For each item, look at each item in set02.

    Compare them. Did you find this item in set02?

    If you didn't find it in set02, add it to set03.

  • Thansk TfGuy44, The problem is that this is pretty straightforward when 'set02' has only 1 element to compare, but if there are multiple values to check against, a nested loop wouldn't work (as far as I can manage)

  • edited May 2018

    If you are trying to use arrays like sets and do set operations on them, and if you aren't particularly attached to vanilla Processing, then one approach is adding a Java Set to your Processing sketch - the interface already has a removeAll() method. It also has toArray() if you need your set to become an array in other parts of your code.

    https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#removeAll(java.util.Collection)

    Of course, if you want to implement it yourself without using Collections, that is fine too!

  • edited May 2018 Answer ✓

    Besides using Java's Collection::removeAll() as @jeremydouglass has suggested:
    https://Docs.Oracle.com/javase/10/docs/api/java/util/Collection.html#removeAll(java.util.Collection)

    We can also rely on Processing's IntList: https://Processing.org/reference/IntList.html

    It's got the "hidden" method removeValues():
    http://Processing.GitHub.io/processing-javadocs/everything/processing/data/IntList.html#removeValues-int-

    And then call its method array() in order to return the result as an int[]:
    https://Processing.org/reference/IntList_array_.html

    /**
     * removeSubArrFromArr() (v1.0.1)
     * GoToLoop (2018/Apr/08)
     *
     * Forum.Processing.org/two/discussion/27935/
     * how-to-remove-values-in-one-array-from-another#Item_4
     */
    
    final int[] set01 = { 1, 2, 3, 4, 5, 6 };
    final int[] set02 = { 1, 2 };
    
    int[] set03;
    
    void setup() {
      set03 = removeSubArrFromArr(set01, set02);
      println(set03); // { 3, 4, 5, 6 }
      exit();
    }
    
    static final int[] removeSubArrFromArr(final int[] arr, final int[] subArr) {
      if (arr == null)  return new int[] {};
      if (subArr == null || subArr.length == 0 || arr.length == 0)  return arr;
    
      final IntList il = new IntList(arr);
      for (final int val : subArr)  il.removeValues(val);
    
      return il.array();
    }
    
  • whoa, never even heard of removeValues()! You've done it again GoToLoop, thanks, this is EXACTLY the kind of thing I was looking for! Thanks as well Jeremy... I should eventually get to using java.util.*... (too scared!)

  • There are many ways to do this in Processing.

    Another option is to use HashMap as described in the reference, but only use the keys -- for instance, adapt the standard reference example to <Integer, Boolean>.

    Note that the reference example includes a java.util import and points straight to the Java docs.

    Notice also that the example already uses one part of the set interfaces to HashMap -- entrySet(). There is also .keySet() for a set interface to just the keys.

    If you don't want to use both keys and values -- just a set of things -- an even easier interface is a HashSet, which does this for you. HashSet doesn't have a page in the Processing reference, but it is just as simple to use as HashMap, and you can work with it in the same way.

    Here are some simple examples of working with a HashSet in Processing(Java):

    import java.util.Arrays;
    import java.util.HashSet;
    
    // create a new set;
    HashSet<Integer> hs = new HashSet<Integer>(Arrays.asList(1, 2, 3));
    println("init:   ", hs);
    
    // add one or more things to the set
    hs.add(4);
    hs.addAll(Arrays.asList(5, 6, 7, 8, 9, 10));
    println("adding: ", hs);
    
    // remove one or more things from the set
    hs.remove(1);
    hs.removeAll(Arrays.asList(3, 5, 7, 9));
    println("remove: ", hs);
    
    // Use an enhanced loop to iterate over each entry
    print("Enhanced loop: ");
    for (Integer me : hs) {
      print(me, ' ');
    }
    println();
    
    // access the set as an array
    print("toArray:  ");
    println(hs.toArray());
    
    // copy the set into a new array
    Integer[] setArray = hs.toArray(new Integer[hs.size()]);
    println("Array[]:");
    println((Object)setArray);
    
    // loop over a copied array
    println("For loop: ");
    for(int i=0; i<setArray.length; i++){
      println(i, setArray[i]);
    }
    

    Output:

    init: [1, 2, 3]
    adding: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    remove: [2, 4, 6, 8, 10]
    Enhanced loop: 2 4 6 8 10
    toArray: 2 4 6 8 10
    Array[]:
    [0] 2
    [1] 4
    [2] 6
    [3] 8
    [4] 10
    For loop:
    0 2
    1 4
    2 6
    3 8
    4 10

Sign In or Register to comment.