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 3674180
In Process

The Archive Base Latest Questions

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

Imagine a std:vector, say, with 100 things on it (0 to 99) currently. You

  • 0

Imagine a std:vector, say, with 100 things on it (0 to 99) currently. You are treating it as a loop. So the 105th item is index 4; forward 7 from index 98 is 5.

You want to delete N items after index position P.

So, delete 5 items after index 50; easy.

Or 5 items after index 99: as you delete 0 five times, or 4 through 0, noting that position at 99 will be erased from existence.

Worst, 5 items after index 97 – you have to deal with both modes of deletion.

What’s the elegant and solid approach?

Here’s a boring routine I wrote

-(void)knotRemovalHelper:(NSMutableArray*)original
         after:(NSInteger)nn howManyToDelete:(NSInteger)desired
    {

#define ORCO ((NSInteger)[original count])

    static NSInteger kount, howManyUntilLoop, howManyExtraAferLoop;

    if ( ... our array is NOT a loop ... )
            // trivial, if messy...
        {
        for ( kount = 1; kount<=desired; ++kount  )
            {
            if ( (nn+1) >= ORCO )
                return;
            [original removeObjectAtIndex:( nn+1 )];
            }

        return;
        }
    else    // our array is a loop
            // messy, confusing and inelegant. how to improve?
            // here we go...
        {
        howManyUntilLoop = (ORCO-1) - nn;

        if ( howManyUntilLoop > desired )
            {
            for ( kount = 1; kount<=desired; ++kount  )
                [original removeObjectAtIndex:( nn+1 )];
            return;
            }

        howManyExtraAferLoop = desired - howManyUntilLoop;

        for ( kount = 1; kount<=howManyUntilLoop; ++kount  )
            [original removeObjectAtIndex:( nn+1 )];

        for ( kount = 1; kount<=howManyExtraAferLoop; ++kount )
            [original removeObjectAtIndex:0];

        return;
        }

#undef ORCO
    }

Update!

InVariant’s second answer leads to the following excellent solution. “starting with” is much better than “starting after”. So the routine now uses “start with”. Invariant’s second answer leads to this very simple solution…

N times do if P < currentsize remove P else remove 0

-(void)removeLoopilyFrom:(NSMutableArray*)ra
    startingWithThisOne:(NSInteger)removeThisOneFirst
    howManyToDelete:(NSInteger)countToDelete
{
// exception if removeThisOneFirst > ra highestIndex
// exception if countToDelete is > ra size

// so easy thanks to Invariant:

for ( do this countToDelete times )
    {
    if ( removeThisOneFirst < [ra count] )
          [ra removeObjectAtIndex:removeThisOneFirst];
    else
          [ra removeObjectAtIndex:0];
    }
}

Update!

Toolbox has pointed out the excellent idea of working to a new array – super KISS.

  • 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-19T02:49:26+00:00Added an answer on May 19, 2026 at 2:49 am

    Another method:

    N times do {remove entry at index P mod max(ArraySize, P)}

    Example:

    N=5, P=97, ArraySize=100
    
    1: max(100, 97)=100 so remove at 97%100 = 97
    2: max(99, 97)=99 so remove at 97%99 = 97  // array size is now 99
    3: max(98, 97)=98 so remove at 97%98 = 97
    4: max(97, 97)=97 so remove at 97%97 = 0
    5: max(96, 97)=97 so remove at 97%97 = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.