given a sorted integer array, return a new integer array without duplicates

Posted on 2004-11-24
Last Modified: 2010-04-17
I have to write a function that given a sorted integer array would return a new integer array without duplicates.
What is the best solution? Is there better solution than looping through the whole array and checking if the value has changed?
Question by:quasar_ee
    LVL 3

    Accepted Solution

    If your array is already sorted, then the best is indeed to loop through it, adding the value to your new array if it differs from the last value.
    LVL 7

    Assisted Solution

    looping is the only way of doing it.

    first read the values in array1
    start creating the new array
    check for the values if they are existing in the new array before adding it
    LVL 84

    Assisted Solution

                          $prev = "not equal to $in[0]";
                           @out = grep($_ ne $prev && ($prev = $_, 1), @in);

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    TextBlock Stops Updating 2 48
    object oriented javascript web form 8 48
    VB 6.0 printer how to align 6 37
    countPairs challenge 7 39
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now