How To Remove Repeated Values (For ith and jth index values)

Hello Professionals,

Good day. I would like to ask how can I create my program more accurate in terms of finding (or saving) the important arrays/values. It's hard to explain what I wanted to do, but I think, my drawing will give a better explanation what I wanted to do.

https://i.imgur.com/HEcawi4.jpg

Let's say I already know the cells I have to save (the cells colored in red), and we have points A,B,C,D,E,F,G,H,I,.... We can say that these points have similarities when it comes to their index values. We can say that points A,B,C have the same index values. points D,E,F have the same index values too. G,H,I have the same index values as well.

Having this idea, we can say that:
A = (0,0)
B = (0,0)
C = (0,0)
D = (0,1)
E = (0,1)
F = (0,1)
G = (0,2)
H = (0,2)
I = (0,2)

My problem is that, I only need one value, I don't need to get repeating values. For example, if I get (0,0) as the first index, I should ignore the next index values holding the same (0,0). So in this context, I need to save only the index values of A, D, and G.

I have found some code which eliminates/removes repeating values, for example:
111222333 -> 123
444333111222 -> 1234
101122003445551 -> 012345


And this is the code for that purpose(which I amended some parts):

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
// list::unique
#include <iostream>
#include <cmath>
#include <list>

// a binary predicate implemented as a function:
bool same_integral_part(double first, double second)
{
	return (int(first) == int(second));
}

// a binary predicate implemented as a class:
struct is_near {
	bool operator() (double first, double second)
	{
		return (fabs(first - second)<5.0);
	}
};

int main()
{
	int myindex[] = { 1,  0, 0,  1,  2,
		0, 1, 3, 5,  4 };
	std::list<int> mylist(myindex, myindex + 10);

	//double mydoubles[] = { 12.15,  2.72, 73.0,  12.77,  3.14,
	//	12.77, 73.35, 72.25, 15.3,  72.25 };
	//std::list<double> mylist(mydoubles, mydoubles + 10);

	mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
							   // 15.3,  72.25, 72.25, 73.0,  73.35

	mylist.unique();           //  2.72,  3.14, 12.15, 12.77
							   // 15.3,  72.25, 73.0,  73.35
	int counter=0;
	for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it) {
		std::cout << ' ' << *it;
		myindex[counter]=*it;
		++counter;
	}
	std::cout << std::endl;
	for (int i = 0; i < counter; ++i) {
		std::cout << ' ' << myindex[i];
	}
	std::cout << '\n';

	mylist.unique(same_integral_part);  //  2.72,  3.14, 12.15
										// 15.3,  72.25, 73.0

	mylist.unique(is_near());           //  2.72, 12.15, 72.25

	std::cout << "mylist contains:";
	for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << '\n';

	system("pause");
	return 0;
}


However, I am having a hard time if I am thinking for two index values as shown above. How can I extend this program for that purpose? Do you have any suggestions? I am sorry if this question is basic for you, but I've been thinking for this problem for about 2 weeks now and until now I can't think for a better solution.

Note: The arrays of the program should be in a "flattened" version (i.e. array[i*n+j], not in a two-dimensional array[i][j].
Last edited on
1
2
3
4
5
6
7
8
9
10
struct Point2{ double x, y; };

int bucket(const Point2 &a, int width){
    //Note: a cast to int is a truncation towards zero, not towards negative infinity.
    return (int)a.x + (int)a.y * width;
}

bool in_same_bucket(const Point2 &a, const Point2 &b){
    return bucket(a) == bucket(b);
}
?

Note that your is_near() isn't transitive. Intransitive relations aren't great for bucketing (or binning), because they make the order in which you add data matter. The behavior gets worse in higher dimensions.
Topic archived. No new replies allowed.