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

  • SEARCH
  • Home
  • 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 6663863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:32:47+00:00 2026-05-26T02:32:47+00:00

I need to add an element to an array only if it is not

  • 0

I need to add an element to an array only if it is not already there in Javascript. Basically I’m treating the array as a set.

I need the data to be stored in an array, otherwise I’d just use an object which can be used as a set.

I wrote the following array prototype and wanted to hear if anyone knew of a better way. This is an O(n) insert. I was hoping to do O(ln(n)) insert, however, I didn’t see an easy way to insert an element into a sorted array. For my applications, the array lengths will be very small, but I’d still prefer something that obeyed accepted rules for good algorithm efficiency:

Array.prototype.push_if_not_duplicate = function(new_element){
    for( var i=0; i<this.length; i++ ){
        // Don't add if element is already found
        if( this[i] == new_element ){
            return this.length;
        }
    }
    // add new element
    return this.push(new_element);
}
  • 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-26T02:32:47+00:00Added an answer on May 26, 2026 at 2:32 am

    If I understand correctly, you already have a sorted array (if you do not have a sorted array then you can use Array.sort method to sort your data) and now you want to add an element to it if it is not already present in the array. I extracted the binary insert (which uses binary search) method in the google closure library. The relevant code itself would look something like this and it is O(log n) operation because binary search is O(log n).

    function binaryInsert(array, value) {
      var index = binarySearch(array, value);
      if (index < 0) {
        array.splice(-(index + 1), 0, value);
        return true;
      }
      return false;
    };
    
    function binarySearch(arr, value) {
      var left = 0;  // inclusive
      var right = arr.length;  // exclusive
      var found;
      while (left < right) {
        var middle = (left + right) >> 1;
    
        var compareResult = value > arr[middle] ? 1 : value < arr[middle] ? -1 : 0;
        if (compareResult > 0) {
          left = middle + 1;
        } else {
          right = middle;
          // We are looking for the lowest index so we can't return immediately.
          found = !compareResult;
        }
      }
      // left is the index if found, or the insertion point otherwise.
      // ~left is a shorthand for -left - 1.
      return found ? left : ~left;
    };
    

    Usage is binaryInsert(array, value). This also maintains the sort of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m reading data from a source as array. a[n] I need to add
I need to add and delete class on an element having multiple classes: This
I need to add an xmlns to the root element in the output of
I need to add a tooltip/alt to a td element inside of my tables
I need to add multiple empty divs to a container element using jQuery. At
I have a GridView that I need to dynamically add TemplateField elements to. My
I need add a new user group for mediawiki. The new group has more
need to add a 2nd css stylesheet to a page. do i add a
I need to add a web part zone to a wiki page. I'm opening
I need to add DVD writing functionality to an application I'm working on. However

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.