How to remove duplicate values in c++

Sorry I am new to all of this, I want to remove duplicate values from "apsk".
if it's needed I'll leave full code in second post. Right now my code doesn't remove all duplicate values.

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
  void rikiuoti(Duomenys A[], int k) // Funkction to sort
{
    string temp;
    for(int i = 0; i < k - 1; i++)
    {
        for(int y = i + 1; y < k; y++)
        {
            if(A[y].apsk > A[i].apsk)
            {
                temp = A[i].apsk;
                A[i].apsk = A[y].apsk;
                A[y].apsk = temp;
            }
        }
    }
}
void skaiciuoti(Duomenys A[], int k) // Function to find duplicate values
{
    for(int i = 0; i < k - 1; i++)
    {
        for(int y = i + 1; y < k; y++)
        {
            if(A[i].apsk == A[y].apsk)
            {
                salinti(k, y);
            }
        }
    } cout << k << endl;
    for(int i = 0; i < k; i++)
    {
      cout << A[i].apsk << endl;
    }
}
void salinti(int & k, int kuris) // Function to remove identical values.
{
    for(int i = kuris; i < k - 1; i++)
    {
        A[i].apsk = A[i+1].apsk;
    }
    k--;
}
Here is full code, but I think it's not necessary

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>
using namespace std;

const char FV[] = "U2.txt";
const char FR[] = "U2rez.txt";

const int Ck = 103, Cgyv = 600000, Cilgis = 20, Cilgis2 = 13;

struct Duomenys
{
    string apsk;
    string miestas;
    int gyv;

}A[Ck];
struct Rezultatai
{
    string apsk;
    int gyv;
}Rez[Ck];

void skaityti(const char FV[], Duomenys A[], int & k);
void rikiuoti(Duomenys A[], int k);
void skaiciuoti(Duomenys A[], int k);
void salinti(int & k, int kuris);
int main()
{
    int k;
    skaityti(FV, A, k);
    rikiuoti(A, k);
    skaiciuoti(A, k);
    return 0;
}
void skaityti(const char FV[], Duomenys A[], int & k) // Function to read "U2.txt";
{
    ifstream fd(FV);
    char a[Cilgis2 + 1], M[Cilgis + 1];
    fd >> k;fd.ignore();
    for(int i = 0; i < k; i++)
    {
        fd.get(M, Cilgis);
        A[i].miestas = M;
        fd.get(a,Cilgis2);
        A[i].apsk = a;
        fd >> A[i].gyv;fd.ignore();
    }
    fd.close();
}

void rikiuoti(Duomenys A[], int k) // Funkction to sort
{
    string temp;
    for(int i = 0; i < k - 1; i++)
    {
        for(int y = i + 1; y < k; y++)
        {
            if(A[y].apsk > A[i].apsk)
            {
                temp = A[i].apsk;
                A[i].apsk = A[y].apsk;
                A[y].apsk = temp;
            }
        }
    }
}
void skaiciuoti(Duomenys A[], int k) // Function to find duplicate values
{
    for(int i = 0; i < k - 1; i++)
    {
        for(int y = i + 1; y < k; y++)
        {
            if(A[i].apsk == A[y].apsk)
            {
                salinti(k, y);
            }
        }
    } cout << k << endl;
    for(int i = 0; i < k; i++)
    {
      cout << A[i].apsk << endl;
    }
}
void salinti(int & k, int kuris) // Function to remove identical values.
{
    for(int i = kuris; i < k - 1; i++)
    {
        A[i].apsk = A[i+1].apsk;
    }
    k--;
}
Have a look at the remove_if function from the algorithm header.
http://www.cplusplus.com/reference/algorithm/remove_if/
closed account (48T7M4Gy)
One strategy would be to change the value of the duplicate element to for example -1 so that it can be ignored in later searches. A separate cleanup routine could then make a new copy of the array as a separate function.

Another possibility along the same lines would be to include a Deleted element in the struct.

(database systems often use this approach and it is more efficient with large arrays than rearranging each time a duplicate occurs)

STL containers, maps etc lend themselves to handling this properly and efficiently but that is beside the point of your question.
One way to do it: http://ideone.com/emiKCU
Note that it will require adjusting to work for your type since the comparison function isn't a point of customization and if you aren't comfortable with pointer arithmetic it'll probably look like gobbledygook.

Also, see http://en.cppreference.com/w/cpp/algorithm/unique (where the comparison function is customizable.)
Topic archived. No new replies allowed.