Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- Developer Tools
- :
- Simple exercicio (novato)

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Simple exercicio (novato)

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.12 01:35 PM

Good evening everyone!

Unfortunately I'm not having the time I would like to study KDB + and Q.

I am at the very beginning of my studies.

Following all indications from the official website (learn section).

I'm still thinking of simple examples to solve (not from the real world).

Can someone check this simple exercise for me?

Given any list, display only items that are not repeated.

l: 1 2 4 5 7 9 2 2 5

Expected response: 1 4 7 9

My first solution:

q) **where 1= count each group l**

q) **1 4 7 9**

I will continue to think about other solutions.

Could someone give me a more "optimized" solution?

Thank you for your help!

11 REPLIES 11

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.13 01:15 AM

For the sake of variety:

q){x where d&1_(d:differ x),1b}asc 1 2 4 5 7 9 2 2 5

1 4 7 9

Sort the list then if an entry is different from the previous entry and different to the next it's unique.

Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.13 01:31 AM

q){x where not d|next d:(~':)x}asc 1 2 4 5 7 9 2 2 5

1 4 7 9

Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.13 11:32 AM

TerryLynch, thank you!

I'm still starting and for that reason I don't know many commands / functions.

I will take advantage of your examples to learn new ways to code.

Em sexta-feira, 13 de novembro de 2020 às 06:15:29 UTC-3, TerryLynch escreveu:

For the sake of variety:q){x where d&1_(d:differ x),1b}asc 1 2 4 5 7 9 2 2 51 4 7 9Sort the list then if an entry is different from the previous entry and different to the next it's unique.Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.14 04:02 AM

q){x where(x<>prev x)&x<>next x}asc 1 2 4 5 7 9 2 2 5

1 4 7 9

The other approach using differ and identical-each-prior would work for general/mixed lists too.

Terry

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.16 01:59 PM

q)l where 1=sum l=/:l

1 4 7 9

q)r where {y*x>=y}':[s 0;1_s,last s:differ r:asc l]

1 4 7 9

-Ajay

-Ajay

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.16 06:21 PM

another way

q){d:distinct x;d where(x bin d)=x binr d} asc l

1 4 7 9

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.17 01:10 AM

to expand on TerryLynch's solution by relaxing the constraint on types we can do

a:{x where differ[x]&differ[first x; next x:asc x]}

this works with nested too

q)a (1 2;2 3; 1 2;4 5;0 0; 0 0)

2 3

4 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.17 01:42 AM

interestingly the fastest solution is hand-roll the frequency collection (ie, count each group)

q)l:100000?10000

q)\ts:100 {d:distinct x;d where(x bin d)=x binr d} asc l

481 2229056

q)\ts:100 where 1=count each group l

239 4064864

q)\ts:100 {x where not d|next d:(~':)x}asc l

187 2229056

q)\ts:100 {d where 1=@[count[d]#0;(d:distinct x)?x;+;1]}l

78 2360128

q)\ts:100 {d where 1i=@[count[d]#0i;(d:distinct x)?x;+;1i]}l

68 1770400

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.17 04:02 AM

building on it you can get away with allocating new object and shorten it further.

q)\t:100 {d where (d+1)=@[d;(d:distinct x)?x;+;1]}l

67

q)\t:100 {d where 1=@[count[d]#0i;(d:distinct x)?x;+;1i]}l

69

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.17 04:47 AM

q)\ts:100 {d:distinct x; d where (x?d)=(-1+count x)-reverse[x]?d}l

131 1442672

q)\ts:100 {d where 1i=@[count[d]#0i;(d:distinct x)?x;+;1i]}l

113 1770528

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2020.11.17 07:05 AM

Caveta, this will only work for simple integer types

Can further improve one of the earlier solutions posted by Attila, using differ only once

q)l:100000?10000

q)\ts:100 {x where differ[x]&differ[first x; next x:asc x]} l

125 3277584

q)\ts:100 {x where (1_d,last d)&d:differ[x:asc x]} l

108 2229008

-Ajay

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.