Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1176

Improve ArrayUtils removeElements time complexity to O(n)

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.4
    • 3.5
    • lang.*
    • None

    Description

      The current ArrayUtils removeElements implementation is not efficient when try to get all indexes that needed to be removed.

      It's O(m*n): n is the length of origin array, m is the unique count of toBeRemoved.

              final BitSet toRemove = new BitSet();
              for (final Map.Entry<Byte, MutableInt> e : occurrences.entrySet()) {
                  final Byte v = e.getKey();
                  int found = 0;
                  for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) {
                      found = indexOf(array, v.byteValue(), found);
                      if (found < 0) {
                          break;
                      }
                      toRemove.set(found++);
                  }
              }
      

      We can just scan the origin array once to get all indexes that needed to be removed.

              final BitSet toRemove = new BitSet();
      
              // the only change here, time complexity changed to O(n)
              for (int i = 0; i < array.length; i++) {
                  final int key = array[i];
      
                  final MutableInt count = occurrences.get(key);
                  if (count != null) {
                      count.decrement();
                      if (count.intValue() == 0) {
                          occurrences.remove(key);
                      }
                      toRemove.set(i);
                  }
              }
      

      Attachments

        Issue Links

          Activity

            People

              pascalschumacher Pascal Schumacher
              yuanyun.cn jefferyyuan
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 24h
                  24h
                  Remaining:
                  Remaining Estimate - 24h
                  24h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified