What’s the computational complexity of the Groovy unique() method?

Question 1

What’s the computational complexity of the Groovy unique() method?

Question 2

How could I have figured it out by myself? The unique() method is defined in the class DefaultGroovyMethods. The source code can be found here: org.codehaus.groovy.runtime.DefaultGroovyMethods. Can you point me to the piece of code which illustrates the answer to question 1?

7

As Lernkurve pointed out in the comments, the implementation given in Groovy’s source code appears to be O(n^2).

public static <T> Collection<T> unique(Collection<T> self, boolean mutate) {
    List<T> answer = new ArrayList<T>();
    for (T t : self) {                    // << Outer loop over each item
        boolean duplicated = false;
        for (T t2 : answer) {             // << Inner loop over each found item
            if (coercedEquals(t, t2)) {
                duplicated = true;
                break;
            }
        }
        if (!duplicated)
            answer.add(t);
    }
    if (mutate) {
        self.clear();
        self.addAll(answer);
    }
    return mutate ? self : answer ;
}

The outer loop ensures that the time complexity is at least O(n), because it will grow linearly as the input list grows. Likewise, the inner loop taken by itself is O(n). For each item in the input list, it then loops over each item that it’s registered into the “answer” list to see if that item is already contained. Put them together and you’ve got an O(n^2) function.

As I pointed out in the comments, there are a few ways you might get better performance out of this, for large sets. If you’re willing to spend a bit more memory, you can use a Hash set of some kind to register whether or not an element has already been included. These generally have an O(1) lookup time to check if the current item is unique to the list.

Alternatively, sorting the input list (if it is sortable) with a good sort function (most of those included in a language’s standard library will be O(n log n)) can help, assuming you don’t care about the order of the “unique” output list. This way you can be sure that duplicate elements will always appear in sequence with each other, and it becomes trivial to reject them from the output.

Indeed complexity is O(n^2). For deeper explanation of how we arrived at O(n^2):

Pretend input is [1 2 3 4 5 6]

Outer loop iteration 1: answer == [ ], t == 1

Outer loop iteration 2: answer == [1], t == 2

Outer loop iteration 3: answer == [1, 2], t == 3

Outer loop iteration 4: answer == [1, 2, 3], t == 4

Outer loop iteration 5: answer == [1, 2, 3, 4], t == 5

Outer loop iteration 6: answer == [1, 2, 3, 4, 5], t == 6

Now sum the number of each inner loop iterations for each of the 6 outer loop iterations:

0 + 1 + 2 + 3 + 4 + 5 = 15

(5 + 0) + (4 + 1) + (2 + 3) = 15

(n/2) * (n - 1) = (n^2 - n)/2 -> O(n^2) (this is the tightest bound)

List<T> answer = new ArrayList<T>();
for (T t : self) {
  boolean duplicated = false;
  for (T t2 : answer) {
    if (comparator.compare(t, t2) == 0) {
      duplicated = true;
      break;
    }
}
if (!duplicated)
  answer.add(t);
}
if (mutate) {
  self.clear();
  self.addAll(answer);
}
return mutate ? self : answer;

I argue that one can write her own algorithm for accomplishing the same thing but in O(n) time, specifically using a Map. First throw all the items into the Map using the specified key, then iterate through all values in the Map. Throwing items into Map is O(n), iterating is also O(n), which equates to O(n) (not O(2n) since the growth is still linear). Not sure why Groovy developers didn’t optimize, unless I’m missing something.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật