Shortening a list.

closed account (91bqM4Gy)
Hello,

I'm a little rusty on my c++. I'm using a c++ based program called root and am trying to analyze some data. I've created a script, but I'm trying to make it more efficient by shortening an array up.

The array consists of multiple numbers, but sometimes the numbers are repeating. I'm trying to get rid of the repetitions so I'm trying to make a function and return an array.

Say it's a list of something like:

656400000
656400000
656400000
25900000
25900000
100000
353400000
656400000
439800000

So sometimes the the numbers will repeat, but not necessarily be consecutive.

Can anyone help me out?

Thanks
Your question is impossible to answer without more information, why don't you just post your code? Make sure to put it [ code ] tags
closed account (91bqM4Gy)
This is what I have so far. Am I on the right track?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void shortenList(Double_t parent) {

//get length of parent array        
        Int_t length = parent->GetSelectedRows();

        track = 0;
        
        bool matchFound = false;
//Search for repetitions
        for (int i=0;i<length;i++) {
                for (int j=0;j<length,j++) {
                        if (parent[i] == newParent[j]) {
                        matchFound = true;
                        break;
                }       
        }
        if (!matchFound)
                newParent[track++] = parent[i];
                matchFound = false;
        }
        return newParent;
}
You could use std::sort() followed by std::unique()

std::sort()
http://cplusplus.com/reference/algorithm/sort/

std::unique()
http://cplusplus.com/reference/algorithm/unique/
Topic archived. No new replies allowed.