Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To remove duplicates and keep order #9976

Open
dlangBugzillaToGithub opened this issue May 21, 2013 · 1 comment
Open

To remove duplicates and keep order #9976

dlangBugzillaToGithub opened this issue May 21, 2013 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-05-21T16:59:50Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=10131

CC List

  • greensunny12
  • razvan.nitu1305

Description

I suggest to add to Phobos a function with semantics similar to this one, that sometimes I find useful:


T[] noDupes(T)(in T[] s) {
    import std.algorithm: canFind;
    T[] result;
    foreach (T c; s)
        if (!result.canFind(c))
            result ~= c;
    return result;
}

void main() {
    import std.string: squeeze;
    assert("AAAA".noDupes == "A");
    assert("AAAA".squeeze == "A");
    assert("ABAC".noDupes == "ABC");
    assert("ABAC".squeeze == "ABAC");
}


Notes:
- It's related to std.string.squeeze and std.algorithm.uniq, but they need the items to be sorted to remove all the duplicates, while the function discussed here doesn't change the order of the items.
- The implementation shown here is slow, O(n^2), because it's meant to just show the semantics clearly. If the items are hashable it's possible to create a O(n) implementation, storing the items in a hash. If the items are comparable it's possible to write a O(n ln n) version that creates an array of sorted pointers and then performs binary searches on it. The O(n^2) version is a fallback if the items are not hashable nor comparable. The three versions are meant to be three parts of the same general function.
@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 2017-10-10T13:15:12Z

PR https://github.com/dlang/phobos/pull/5774

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants