# Decision Optimization

View Only

## Filter Tupleset to Remove Elements with Duplicate Entries in a Field

• #### 1.  Filter Tupleset to Remove Elements with Duplicate Entries in a Field

Posted Fri March 27, 2020 07:51 PM

Originally posted by: tjnelso

I have a tupleset like the following one:

```tuple exampleRow {string a; string b; string c;};
{exampleRow} exampleSet = {
<"One", "Square", "A">
<"Two", "Square", "B">
<"One", "Circle", "A">
<"Two", "Triangle", "C">
};
```

I would like to create a new tupleset from this one which removes rows that have duplicate entries in the 'b' field. In this example, that means removing the first two tuples because field 'b' is "Square" for both of them. I would then like to use the filtered version of this tupleset in a forall() constraint.

```tuple exampleRow {string a; string b; string c;};
{exampleRow} filteredExampleSet = {
<"One", "Circle", "A">
<"Two", "Triangle", "C">
};

forall(<a,b,c> in filteredExampleSet)
{
...
}
```

I've tried to use the card() function but am not able to find a formulation that gives me what I need. I've also looked into the count() function without luck, since it accepts only integers. Thank you.

• #### 2.  Re: Filter Tupleset to Remove Elements with Duplicate Entries in a Field

Posted Sat March 28, 2020 01:06 PM

Hi

tuple exampleRow {string a; string b; string c;};

{exampleRow} exampleSet = {
<"One", "Square", "A">,
<"Two", "Square", "B">,
<"One", "Circle", "A">,
<"Two", "Triangle", "C">
};

{string} setofb={i.b | i in exampleSet};

int nbofb[i in setofb]=card({j | j in exampleSet : j.b==i});

{exampleRow} exampleSet2={i | i in exampleSet : nbofb[i.b]==1};

execute
{
writeln("exampleSet2 = ",exampleSet2);
}

gives

exampleSet2 =  {<"One" "Circle" "A"> <"Two" "Triangle" "C">}

regards

PS:

• #### 3.  RE: Re: Filter Tupleset to Remove Elements with Duplicate Entries in a Field

Posted Thu May 07, 2020 04:31 AM

Hi

tuple exampleRow {string a; string b; string c;};

{exampleRow} exampleSet = {
<"One", "Square", "A">,
<"Two", "Square", "B">,
<"One", "Circle", "A">,
<"Two", "Triangle", "C">
};

{string} setofb={i.b | i in exampleSet};

int nbofb[i in setofb]=card({j | j in exampleSet : j.b==i});

{exampleRow} exampleSet2={i | i in exampleSet : nbofb[i.b]==1};

execute
{
writeln("exampleSet2 = ",exampleSet2);
}

gives

exampleSet2 =  {<"One" "Circle" "A"> <"Two" "Triangle" "C">}

regards

PS: