remove duplicates from 2d array

hi,

i have a 2d string array string array[j][47];. For the elements array[j][3] i have duplicate values. I would like to create a new array taking-copying only the distinct values of array[j][3].

So if i have :
1
2
3
4
5
6
A A A a A A  
B A A a A A
C A A a A A
D A A b A A
E A A a A A
F A A c A A

i want to take:
1
2
3
a
b
c

i'm not interested in vector solutions, it must be done with arrays.

thanks in advance.
Iterate over all values in array[j][3]. For each element check if it exist in your new array. If not — copy element to new array.
Your example shows only chars. Do you have strings with more than one char in them?
I was bored and created a sample program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>
#include <algorithm>

int main()
{
    const int j = 5;
    std::string array[j][47] = {{"wall","brick","stone","first"},
                                {"wall","brick","stone","first"},
                                {"wall","brick","stone","six"},
                                {"wall","brick","stone","first"},
                                {"wall","brick","stone","duo"}};

    std::array<std::string, j> temp, out;
    for(int i = 0; i < j; ++i){
        temp[i] = array[i][3];
    }
    std::sort(temp.begin(), temp.end());
    auto last = std::unique_copy(temp.begin(), temp.end(), out.begin());
    for(auto i = out.begin(); i != last; ++i)
        std::cout << *i << " ";

    return 0;
}

Output:

duo first six
Last edited on
Topic archived. No new replies allowed.