Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Suppose I have a array and have to search for a particular element consider number 12.if number 12 occurs in the array 3 times then I will have to delete all the occurences of the element and then reduce the size of the array(since here 12 occurs 3 times and initial size of array is 10 then now after deletion of 12 array size should be 7).Is it possible to reduce the size of an array in this way dynamically? You can't create or use a new array.
Posted

I'm not sure which language you are actually using but in C++ you could use the STL vector[^] class, which allows dynamic insertion and deletion. In Java you should probably use one of the Collections[^] classes.
 
Share this answer
 
Comments
LaxmikantYadav 16-Mar-11 8:25am    
Dear Richarad, Thanks for your reply but i dont want to use STL or any built in classes. It should be done using only Array.
Richard MacCutchan 16-Mar-11 9:24am    
Fine, but normal arrays in either language are not dynamic so you will have to write your own class to emulate what vector does already.
markkuk 16-Mar-11 8:27am    
Why not use STL? Is this a typical homework assignment with nonsensical restrictions?
LaxmikantYadav 16-Mar-11 8:31am    
No It is not assigenment, I just want to know how to implement this.
Olivier Levrey 16-Mar-11 9:10am    
Right. My 5. Why re-inventing the wheel?
It's easy in C++ if you use a std::vector instead of a plain array.
#include <vector>
#include <algorithm>
// this isn't a complete program you could compile, just a code snippet
std::vector<int> myvec(10);
// let's pretend myvec has been filled with suitable values, to remove any
// twelves:
myvec.erase(std::remove(myvec.begin(), myvec.end(), 12), myvec.end);
 
Share this answer
 
v3
Comments
LaxmikantYadav 16-Mar-11 8:27am    
Thanks for your reply but i dont want to use STL or any built in classes. It should be done using only Array..
CPallini 16-Mar-11 8:46am    
What is 'Array'? Standard C++ hasn't it.
Varad Velingkar 16-Mar-11 8:55am    
but i don't think it is possible to reduce the size of an array once it is allocated.Isn't it
Olivier Levrey 16-Mar-11 9:13am    
Voted 5. This is not possible to change the size of a fixed size buffer or a buffer allocated with new, but you can use the realloc function to change the size of a buffer allocated with malloc for example. Note that the resulting pointer will probably be different from the original one.
LaxmikantYadav 16-Mar-11 9:01am    
Ok, I want to know it is possible to reduce size of array once it is allocated ??
If you really don't want to use std::vector, for POD types[^] you can use realloc[^] to resize the array after removing an element.

Of course, under the hood realloc is often copying the array and invokes pretty much the same "magic" as vector, so I don't know what you would really achieve with it, but it is an option.
 
Share this answer
 
Comments
fjdiewornncalwe 16-Mar-11 9:24am    
+5. I think this is what the OP's instructor is after. This sounds like a school assignment designed to give understanding into how an array really works.
Sergey Alexandrovich Kryukov 16-Mar-11 19:25pm    
Probably you're right, my 5 to Nemanja.
--SA
LaxmikantYadav 17-Mar-11 0:52am    
My +5 for sloving the problem :)
The answers provided by Richard and markkuk are correct.

I don't know what you mean with "Array".
- If you want to modify the size of a fixed size buffer, then this is not possible.
- If you want to change the size of a dynamic allocated buffer allocated with malloc or calloc, then you can use realloc. Note that the resuling pointer will probably be different from the original one.
- If you want to change the size of a dynamic allocated buffer allocated with new, then you can write your own realloc function using new.

But as the others already told you, I suggest you use STL instead of realloc.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Mar-11 19:25pm    
Valid points, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900