1

Let's say I have an array of integers

1  6 6  3 3  8  4 4

It will be always of the form n*(pairs of number) + 2 (unique numbers).

Is there an efficient way of keeping only the 2 uniques values (i.e. the 2 with single occurence)?

Here, I would like to get 1 and 8.

So far is what I have:

SELECT node_id 
FROM 
( SELECT node_id, COUNT(*) 
  FROM unnest(array[1,  6, 6 , 3, 3 , 8 , 4 ,4]) AS node_id 
  GROUP BY node_id 
) foo 
ORDER BY count LIMIT 2;
Denis Rouzaud
  • 2,412
  • 2
  • 26
  • 45

1 Answers1

3

You are very close, I think:

SELECT node_id 
FROM (SELECT node_id, COUNT(*) 
      FROM unnest(array[1,  6, 6 , 3, 3 , 8 , 4 ,4]) AS node_id 
      GROUP BY node_id 
      HAVING count(*) = 1
     ) foo ;

You can group these back into an array, if you like, using array_agg().

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786