We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The part where people get it wrong is that only one record of 1,1 will not generate a symmetric pair. There has two be repeating lines of 1,1 and 1,1.
In the dataset there is only 13, 13 and 13, 13 that fullfills this.
you have to treat non identical records and repeating identical records in separate queries.
SELECT DISTINCT non_duplicates.X, non_duplicates.Y
FROM (
SELECT X, Y
FROM FUNCTIONS
WHERE X != Y
) AS non_duplicates
JOIN FUNCTIONS AS F2 ON non_duplicates.X = F2.Y
WHERE non_duplicates.Y = F2.X
AND non_duplicates.X <= non_duplicates.Y
UNION
SELECT X, Y
FROM FUNCTIONS
WHERE X = Y
GROUP BY X, Y
HAVING COUNT(*) > 1
ORDER BY X ASC;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Symmetric Pairs
You are viewing a single comment's thread. Return to all comments →
The part where people get it wrong is that only one record of 1,1 will not generate a symmetric pair. There has two be repeating lines of 1,1 and 1,1. In the dataset there is only 13, 13 and 13, 13 that fullfills this. you have to treat non identical records and repeating identical records in separate queries.
SELECT DISTINCT non_duplicates.X, non_duplicates.Y FROM ( SELECT X, Y FROM FUNCTIONS WHERE X != Y ) AS non_duplicates JOIN FUNCTIONS AS F2 ON non_duplicates.X = F2.Y WHERE non_duplicates.Y = F2.X AND non_duplicates.X <= non_duplicates.Y
UNION
SELECT X, Y FROM FUNCTIONS WHERE X = Y GROUP BY X, Y HAVING COUNT(*) > 1
ORDER BY X ASC;