Code snippets
Code snippets
Copy-ready answers to everyday coding questions, each with a short explanation. Laravel, PHP, JavaScript, SQL, Bash and more.
3 snippets tagged #set · Show all
SQL
October 20, 2026
MySQL get the Nth highest salary using LIMIT OFFSET
A classic interview question. DISTINCT skips ties, and OFFSET N-1 jumps to the Nth value. This example returns the 2nd highest.
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1; -- change the offset for the 3rd, 4th, etc.
JavaScript
October 14, 2026
JavaScript remove duplicates from an array
A Set keeps only unique values, so spreading it back into an array removes duplicates. For objects, dedupe by a key with a Map.
const nums = [1, 2, 2, 3, 3, 3];
const unique = [...new Set(nums)];
console.log(unique); // [1, 2, 3]
JavaScript
October 8, 2026
Union, intersection and difference of Sets with built-in Set methods
ES2025 adds set-algebra methods to Set, so you no longer write spread-and-filter helpers. They return new Sets. Needs Node 22+, Chrome 122, Firefox 127 or Safar...
const a = new Set([1, 2, 3]), b = new Set([3, 4]);
console.log(a.union(b), a.intersection(b), a.difference(b), a.symmetricDifference(b));
console.log(new Set([3]).isSubsetOf(a));
// Set(4) { 1, 2, 3, 4 } Set(1) { 3 } Set(2) { 1, 2 } Set(3) { 1, 2, 4 }