Array problem

Hello! I am trying to find a way to do something like this:
input: 3 4 7 4 3 3 7
output: 3 4 7

So what I am trying to do is from an array of integers to take numbers that occur only once. If 3 is in that away I am trying to input it in a different array only once.

input: 8 3 2 9 8 9 2
output: 2 3 8 9

I cannot find a way to solve this, and I have been trying to solve it for a long time.
closed account (3TXyhbRD)
It looks like you want your output to be sorted. If this is the case, there are enough sorting algorithms online to help you out. I would personally go for bubble/insertion sort and specialize it a bit. When you want to insert a new element you will need to go through, at most, all elements to find the right spot. If that element already exists don't insert it (one extra if in the standard algorithm).

Example (for input: 8 3 2 9 8 9 2):
 ``` Result array = [ ] Read number (8) Result array = [ ] ^ (beginning of array => insert) Result array = [ 8 ] Read number (3) Result array = [ 8 ] ^ (is 3 greater than 8? No => move to a lower position) Result array = [ 8 ] ^ (beginning of array => insert) Result array = [ 3, 8 ] ... (inserting 2 and 9 using the same fashion) Result array = [ 2, 3, 8, 9 ] Read number (8) Result array = [ 2, 3, 8, 9 ] ^ (is 8 greater than 9? No => move to a lower position) Result array = [2, 3, 8, 9 ] ^ (is 8 equal to 8? Yes => don't insert) Result array = [ 2, 3, 8, 9 ] ... (same fashion for the rest) ```

I will try it out and see what I can do, thanks!
Last edited on
This sounds like you need to use a sorted set.
http://www.cplusplus.com/reference/set/set/
Last edited on
 ``12345678910111213141516171819202122232425262728`` ``````#include #include using namespace std; int main() { long Arr[] = {3,4,7,4,3,3,6,4,6,8,9,4,3,2,5,1,5,9}; int size = (int)(sizeof Arr / sizeof Arr[0]); long temp[size]; sort(Arr, Arr + size); // for (int h = 0; h < size; ++h) // cout << Arr[h] << " " << h <

 ```\$ ./File 1 2 3 4 5 6 7 8 9```
Last edited on
Personally I would've stored the numbers in a map<unsigned, unsigned>. The map is sorted naturally and you have bonus of being able to store how many of each you find.

e.g
 ``123456789101112131415161718192021`` ``````map data; unsigned input = 0; // read input data[input]++; // read input data[input]++; // in C++11 you could do: cout << "Output: " for (auto i = data.begin(); i != data.end(); ++i) cout << i->first << " "; cout << endl; // In C++01 you could do: map::iterator i; cout << "Output: " for (i = data.begin(); i != data.end(); ++i) cout << i->first << " "; cout << endl;``````
I think that you have not to change the original array. In this case the approach is simple. Before adding a new element from the source array to the destination array you should check that there is no such element in the destination array.:)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````/* code by usandfriends */ #include main() { int howmany,i,n,j,counter,lowest,highest,tmp; lowest=0; highest=0; std::cout<<"How long is your list? "; std::cin>>howmany; std::cout<>tally[i]; if(lowest>=tally[i]) { lowest=tally[i]; } if(highest<=tally[i]) { highest=tally[i]; } } std::cout<tally[j-1]) {;} else { tmp=tally[j]; tally[j]=tally[j-1]; tally[j-1]=tmp; } } } for(i=0;i
Last edited on
If to use sorting then the algorithm is following.
1). You copy the original array in the destination array.
2). Sort the destination array.
3) Apply standard algorithm std::unique.

For example

 ``12345678910111213141516171819202122232425`` ``````#include #include #include int main() { int a[] = { 1, 7, 3, 3, 1, 6, 5, 7, 4, 6 }; int b[sizeof( a ) / sizeof( *a )]; std::copy( std::begin( a ), std::end( a ), std::ostream_iterator( std::cout, " " ) ); std::cout << std::endl; std::copy( std::begin( a ), std::end( a ), std::begin( b ) ); std::sort( std::begin( b ), std::end( b ) ); auto last = std::unique( std::begin( b ), std::end( b ) ); std::copy( std::begin( b ), last, std::ostream_iterator( std::cout, " " ) ); std::cout << std::endl; std::cout << "The new array contains " << std::distance( std::begin( b ), last ) << " unique elements." << std::endl; }``````

The output is

 1 7 3 3 1 6 5 7 4 6 1 3 4 5 6 7 The new array contains 6 unique elements.

But as I said early all these approaches based on sorting have one serious shortage of breaking the original order of elements in the array.

So I would prefer the approach I described early in my previous post.

EDIT: In the presented code the two statements

 ``123`` `````` std::copy( std::begin( a ), std::end( a ), std::begin( b ) ); std::sort( std::begin( b ), std::end( b ) );``````

could be substituted for one statement

` std::partial_sort_copy( std::begin( a ), std::end( a ), std::begin( b ), std::end( b ) );`
Last edited on
Topic archived. No new replies allowed.