# Getting best combination of unique possibilities RSS

## 6 replies

Last post Aug 14, 2019 03:47 PM by bruce (sqlwork.com)

• Loevbo

Member

1 Points

8 Posts

### Getting best combination of unique possibilities

I have an arr like this below, that contains "rows" with 15 numbers inside.

```    var arr = [
[4, 5, 11, 18, 22, 31, 39, 42, 51, 58, 62, 70, 77, 82, 83],
[2, 17, 18, 19, 26, 30, 35, 41, 52, 58, 65, 69, 77, 87, 88],
[1, 8, 12, 14, 21, 24, 33, 40, 45, 59, 61, 77, 78, 80, 81],
[2, 4, 13, 27, 28, 36, 39, 48, 56, 62, 65, 72, 74, 87, 89],
[9, 16, 28, 29, 33, 47, 49, 52, 55, 57, 69, 70, 72, 87, 89],
[1, 2, 11, 13, 29, 32, 36, 42, 45, 49, 67, 69, 71, 81, 82],
[6, 7, 17, 23, 31, 35, 40, 50, 56, 66, 72, 73, 74, 88, 89],
[3, 7, 17, 27, 28, 30, 48, 49, 51, 61, 63, 71, 75, 81, 82]
]```

Let's say I can only choose 3 "rows", then I wish to get the 3 rows that offer the best combination of unique numbers, so In the end i have the lowest combination of duplicated numbers.

I've tried now for 2 days to write a piece of code that can calculate this, but I've got so frustrated I've deleted all I have and now I look for some help to figure this out.

I hope someone can point me in the right direction.

• Yang Shen

Contributor

2910 Points

928 Posts

### Re: Getting best combination of unique possibilities

Aug 14, 2019 09:25 AM|Yang Shen|LINK

Hi Loevbo,

#### Loevbo

the 3 rows that offer the best combination of unique numbers

Can you describe what is the best combination of unique numbers? Is there any condition?

For example, in your arr, which three are with the the best combination of unique numbers?

Best Regard,

Yang Shen

• Loevbo

Member

1 Points

8 Posts

### Re: Getting best combination of unique possibilities

It's a bit hard to figure our what is the best because I would have to go by it number by number.

but if we cut it down to "2" rows then I think it's

```[2, 17, 18, 19, 26, 30, 35, 41, 52, 58, 65, 69, 77, 87, 88],
[1, 8, 12, 14, 21, 24, 33, 40, 45, 59, 61, 77, 78, 80, 81],```

• Yang Shen

Contributor

2910 Points

928 Posts

### Re: Getting best combination of unique possibilities

Aug 14, 2019 10:01 AM|Yang Shen|LINK

Hi Loevbo,

Let's make sure two points:

1. In each row, there's no duplicate numbers.
2. In each row, all the numbers are arranged in order from small to large.

Right?

And you are looking for the 3 rows with most unique numbers in them(the 3 rows)?

Best Regard,

Yang Shen

• Loevbo

Member

1 Points

8 Posts

### Re: Getting best combination of unique possibilities

1.  There is no duplicated numbers in each row
2. They are arranged from small to large as you see in the example.

I want to find x amount of rows that contains as few as possible duplicated numbers in total.

• PatriceSc

All-Star

47090 Points

17345 Posts

### Re: Getting best combination of unique possibilities

Hi,

Looks like homework and the whole point is likely about learning to break a problem into smaller problems. Also rather than deleting your code, you could use the VS debugger to run your code step by step and understand why it doesn't work which is also part the learning process.

Here it seems you could :
- browse each array once to update a count for each number to know how many times it is used
- you can then sum those values to know for each array how  many duplicated numbers are used (a bit unclear if the actual count does matter or if you just care about a number being duplicated)

And then you finally take the x rows having the better score. In  short think first about each small step you need to get the final result rather than keeping having just a rough overview of the problem or trying to start coding without a plan in mind...

Once you have done something that works with a basic idea you can then see if you have room for optimization...

• bruce (sqlwo...

All-Star

55564 Points

14283 Posts

### Re: Getting best combination of unique possibilities

Aug 14, 2019 03:47 PM|bruce (sqlwork.com)|LINK

you should break the problem into multiple steps.

step one, generate all the combinations of x rows. you should be able to optimize this step with a little thinking. just create another array of all the combinations of x rows. say its three, you should be able to create an array of arrays

var combos = [[1,2,3], [1,2,4]....]

step two. loop thru the combos array and calc the number of unique values.

var uniqueCountsPerCombo [10,11,10,....]

step three. find the max value in uniqueCountsPerCombo, this will be the index of the combos.

step four. decide what to do with ties (first, last, random, all).

bruce (sqlwork.com)