eliminating repeatation value in array


hi, i m writing code to eliminate the repeating in array.
example :
array[10]= {1,2,3,4,5,6,7,3,8,9}
output :
1 2 3 8 9 // repeatation between 3 is eliminate
array[10]= {1,2,3,4,5,6,4,7,2,9}
output is :
1 2 9 // the repeatation between 4 is eliminate

However, i face the problem in this example:
array[10]= {1,2,3,4,5,3,7,6,7,9}
output is :
1 2 3 7 6 7 9 //the repeatation between 3 is eliminate, but in between 7 is not.

Thank you very much

the code:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include <iostream>

using namespace std;
main(){
	   
	   
	   
     int numb[100];
   	 int numb_temp[100];
	 
	 int i, j, loop, c, n;
	 
   cout<<"Enter number of elements in array\n"<<endl;
   cin>>n;
 
   cout<<"Enter "<< n <<" elements\n"<<endl;
 
   for ( c = 0 ; c < n ; c++ ){
      cin>>numb[c];}
	 

	 for(i=0; i<n; i++)
	 {
	  	   numb_temp[i] = numb[i];
	  	   
         for(j=i; j<n; j++)
	  	   { 
		   			if(numb_temp[i] == numb[j+1])
					{ 
					  loop = n - ((j+1) - i);				
					  for(int k=j+1; k<=(n-1); k++)
					  { numb[i++]=numb[k]; }
					  	 break;
				    }
					
					else continue; 
					   }
					   
			 }
  
			 
	 cout<<"The Result: "<<endl;
	 for(i=0; i<loop; i++)
	 {
	  		  cout<<numb[i]<<endl;
				}
	 system("pause");
	 return 0;
	 }

Last edited on
Lets recap:
A) If a value is found twice *, then the second element and all elements in between are removed.
B) repeat A until there are no repeats.

*) What if there are multiple repeats of same value {1,7,2,7,3,7,4}? Conservative {1,3,7,4} or greedy {1,4}?

Loop on line 23 is a good start, but you loop to original 'n', even when erasing a range shrinks the array.
You look at element X
IF same value is found (position Y) from the rest of the array [X+1,end[
THEN move tail of array [Y+1,end[  to position X+1 and update 'n'


PS. Do check on line 16 that 0 <= n && n <= 100. Better yet, check all input for failures.
Last edited on

I would say that you need a function that you repeatedly call to find the last index for the number you are searching for and then to carry on from that index until you reach the end:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <vector>
#include <algorithm>

using namespace std;

vector<int> input;


int findLastIndex(const int value)
{
   int index(input.size());
   bool found(false);

   for (vector<int>::reverse_iterator itr = input.rbegin(); itr != input.rend() && !found; ++itr)
   {
	--index;
	if (*itr == value)
	{
	   break;
	}
   }

   return index;
}

int main(int argc, char **argv)
{
   int count;
   cout << "Enter number of elements in array\n"<<endl;
   cin >> count;

   cout<<"Enter "<< count <<" elements\n"<<endl;

   for (int n = 0; n < count; ++n)
   {
	int num;
	cin >> num;
	input.push_back(num);
   }

   vector<int> out;
   int idx(0);

   do
   {
	idx = findLastIndex(input.at(idx));

	if (idx < input.size())
	{
	   out.push_back(input.at(idx));
	   idx++;
	}
   } while (idx < input.size());

   cout<<"The Result: "<<endl;

   for (auto itr = out.begin(); itr != out.end(); ++itr)
   {
	cout << *itr;
   }

   return 0;
}
Keskiverto :
For array { 1,7,2,7,3,7,4} the out put will be 1 7 4

Ajh32:
Can u show without Any library?? Because i want to use this for arduino btw, and they didn't provide those library.... :)

Thank very much for your kindness and help, its help me a lot...
My bad, I erased the first occurrence by mistake. Previous post updated.

Greedy it is. The only thing that has to be adjusted is the "find"; start from end (n-1).


How come that "arduino" has iostream but not the other standard library components? (Rhetoric question.)

Even though you don't have these libraries, you can still read their documentation, for many pages (on this site) have examples on how the library could be implemented. Look at the ideas and adapt.

Untested:
1
2
3
4
5
6
7
8
9
10
11
12
for ( size_t pivot = 0; pivot < count; ++pivot ) {
  size_t pos = count;
  do {
    --pos;
    if ( arr[pos] == arr[pivot] ) break;
  } while ( pivot < pos );

  if ( pivot < pos ) {
    // copy range arr[pos+1]..arr[count] to arr[pivot+1]..
    count -= pos - pivot;
  }
}
Last edited on
Hahahaa...i will change this code to arduino code and implement i...
Thank you so much mate.... U guys help me a lot :)

I would be surprised if you can't get a STL for the arduino. Using the STL makes it much easier for you to program, you haven't got to worry about memory allocation for the correct size of the arrays in your program. It's not a good idea to setup arrays and an arbitrary size.
If you want a solution to your problem without using the STL, here you go. If you are allocating memory on the heap make sure your delete it when no longer required and before your pointers go out of scope. Failure to delete pointers will result in memory leakage:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

using namespace std;

int findLastIndex(const int value, int* input, const int size)
{
   int index(size);
   bool found(false);

   for (int idx = size-1; idx >= 0; --idx)
   {
	--index;
	if (input[idx] == value)
	{
		break;
	}
   }

   return index;
}

int main(int argc, char **argv)
{
   int count;
   cout << "Enter number of elements in array\n"<<endl;
   cin >> count;

   cout<<"Enter "<< count <<" elements\n"<<endl;

   int* input = new int[count];

   for (int n = 0; n < count; ++n)
   {
	int num;
	cin >> num;
	input[n] = num;
   }

   int* out = new int[count];
   memset(out, 0, count);
   int in_idx(0), out_idx(0);

   do
   {
	in_idx = findLastIndex(input[in_idx], input, count);

	if (in_idx < count)
	{
		out[out_idx] = input[in_idx];
		in_idx++;
		out_idx++;
	}
   } while (in_idx < count);

   cout<<"The Result: "<<endl;

   for (int idx = 0; idx < out_idx; idx++)
   {
	cout << out[idx];
   }

   delete[] out;
   delete[] input;

   return 0;
}
Last edited on

Did that help? Would be nice to know!
sorry mate i just reply it... :)

ya its working :) thank you so much...
thank you... its really help me a lot :)
Topic archived. No new replies allowed.