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;

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.