Deleting all occurances of a number in an array

Hey, so I have to delete all occurrences of a number in an array, without using vectors, strings or pointers.I understand that we can't really "Delete" the number since the array isn't dynamic.

What my thought process is that I loop through the array, if the array[i] matches the element we are trying to delete, then shift all of the numbers to the right of that, left one space.

This sounds a lot easier when I'm typing it out but I'm struggling right now with how to actually put it into work.

This is what I currently have.
n is the size
x is the number we are trying to delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  // looping through the array
  for (i = 0; i < n; i++)
	{
                //if we find the number
		if (a[i] == x)
		{
                        // start at that number and go right until end of array
			for (int j= x; j <= n - 1; j++)  
			{
                                //shift those items to the left 1?
				a[j] = a[j + 1];	
			}
		}
       }


The output is currently looking like:
array =[1,2,3,4,5] - delete the number 3 - [1,2,3,5]
array =[1,2,2,3,4] - delete the number 2 - [1,2,3,4]

All help is appreciated, once again I can't use anything "Fancy" Just pretty much loops.
Last edited on
x is a number, not an array index.
But then, on line 8, you initialize j to be x, and use it as an array index.
Perhaps you want to initialize j as i, or something of that nature.
if the order of the array is not critical, you can have something like (pseudocode)

if (found value at x)
{
swap(array[x], array[end])
end--; //end is the index of the last active record.
}
this costs much less than shifting all the values every time. But if the array is sorted or something, you can't do this.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

//==========================================================

void remove( int A[], int &n, int value )
{
   int nextWrite = 0;
   for ( int nextRead = 0; nextRead < n; nextRead++ )
   {
      if ( A[nextRead] != value ) A[nextWrite++] = A[nextRead];
   }
   n = nextWrite;
}

//==========================================================

void print( int A[], int n )
{
   for ( int i = 0; i < n; i++ ) cout << A[i] << " ";
   cout << '\n';
}

//==========================================================

int main()
{
   int A[] = { 1, 2, 3, 3, 4, 5, 2, 6, 7, 2 };
   int n = sizeof A / sizeof A[0];
   print( A, n );
   remove( A, n, 2 );
   print( A, n );
}

//========================================================== 

1 2 3 3 4 5 2 6 7 2 
1 3 3 4 5 6 7

@lastchance

thanks for your help! Just a question, what is nextwrite supposed to represent? Like I don't understand what it's doing in this case. I understand that nextRead is equivalent to 'i' but idk about next write.
The code simply moves two indices forward - nextRead goes through each element in turn; nextWrite is the next available place into which to put an acceptable value.

The names of variables (i or nextRead) are irrelevant. I happen to think that the names here express more obviously (at least, to me) what the variables are intended to represent.
Last edited on
Registered users can post here. Sign in or register to post.