Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:30:58+00:00 2026-05-30T08:30:58+00:00

Just in case you missed, the question is about deleting duplicates on a sorted

  • 0

Just in case you missed, the question is about deleting duplicates on a sorted array. Which can be applied very fast algorithms (compared to unsorted arrays) to remove duplicates.

  • You can skip this if you already know how deleting duplicates on SORTED arrays work

Example:

var out=[];
for(var i=0,len=arr.length-1;i<len;i++){
    if(arr[i]!==arr[i+1]){
        out.push(arr[i]);
    }
}
out.push(arr[i]);

See?, it is very fast. I will try to explain what just happened.

The sorted arrays *could look like this:

arr=[0,1,1,2,2,3,4,5,5,6,7,7,8,9,9,9];

*the sorting could be ASC or DESC, or by other weird methods, but the important thing is that every duplicated item is next each other.

We stopped at array.length-1 because we don’t have anything to check with

Then we added the last element regardless of anything because:

case A:

... ,9,9,9];//we have dup(s) on the left of the last element

case B:

... ,7,9,10];//we don't have dup(s) on the left of the last element

If you really understand what is happening, you will know that we haven’t added any 9 on the case A. So because of that, we want to add the last element no matter if we are on case A or B.


Question:

That explained, I want to do the same, but ignoring the undefined value on cases like:

var arr=[];arr[99]=1;//0 through 98 are undefined, but do NOT hold the undefined value

I want to remove those. And on the case I have some real undefined values, these should not be removed.

My poor attempt is this one:

var out=[];
for (var i=0,len=arr.length; i < len - 1;) {
  var x = false;
  var y = false;

  for (var j = i, jo; j < len - 1; j++) {
    if (j in arr) {
      x = true;
      jo = arr[j];
      i = j + 1;
      break;
    }
  }
  if (x == false) {
    break;
  }

  for (var u = i, yo; u < len - 1; u++) {
    if (u in arr) {
      y = true;
      yo = arr[u];
      i = u + 1;
      break;
    }
  }
  if (y == false) {
    out.push(jo);
    break;
  }

  if (jo !== yo) {
    out.push(jo);
  }
}
out.push(arr[len - 1]);

I am really lost, any help is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:31:00+00:00Added an answer on May 30, 2026 at 8:31 am

    For a start, I’m not entirely certain your original code is kosher. It appears to me that it may not work well when the original list is empty, since you try to push the last element no matter what. It may be better written as:

    var out = [];
    var len = arr.length - 1;
    if (len >= 0) {
        for (var i = 0;i < len; i++) {
            if (arr[i] !== arr[i+1]) {
                out.push (arr[i]);
            }
        }
        out.push (arr[len]);
    }
    

    As to your actual question, I’ll answer this as an algorithm since I don’t know a lot of JavaScript, but it seems to me you can just remember the last transferred number, something like:

    # Set up output array.
    
    out = []
    
    # Set up flag indicating first entry, and value of last added entry.
    
    first = true
    last = 0
    
    for i = 0 to arr.length-1:
        # Totally ignore undefined entries (however you define that).
    
        if arr[i] is defined:
            if first:
                # For first defined entry in list, add and store it, flag non-first.
    
                out.push (arr[i])
                last = arr[i]
                first = false
            else:
                # Otherwise only store if different to last (and save as well).
    
                if arr[i] != last:
                    out.push (arr[i])
                    last = arr[i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All my files can be found on GitHub: https://github.com/Integralist/Passage (just in case you need
I use VS2008 targetting .NET 2.0 Framework, and, just in case, no I can't
I'm not very familiar with CoreAnimation, so I hope I've just missed something pretty
Hoping this is just a case of syntax. I'm writing a custom search function
I'm pretty sure the answer is no but just in case: For a stand
I suspect the answer is probably no, but just in case, is there a
UPDATE: I have somewhat resolved the issue. Just in case if anyone runs in
I'm guessing the answer is no, but just in case, I'm curious if there's
Is it possible to hide the folders in my root folder? Just in case
I've just seen this case class in the Scala actors package: case class !

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.