847 GET /api/users
623 GET /api/health
412 POST /api/login
Note uniq only removes adjacent duplicates, so you almost always need to sort first. -c prefixes each line with its count, -d shows only duplicates, -i ignores case.
When to use:
- Sorted array pair problems
- Removing duplicates in place
- Container with most water
- Palindrome checking
Variants:
- Opposite ends (converging)
- Same direction (fast/slow or lagging)
example
// JavaScript — Remove duplicates from sorted arrayfunction removeDuplicates(arr) {
if (arr.length === 0) return0;
let writeIdx = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] !== arr[i - 1]) {
arr[writeIdx] = arr[i];
writeIdx++;
}
}
return writeIdx;
}
# Pythondef remove_duplicates(arr):
ifnot arr:
return0
write_idx = 1for i in range(1, len(arr)):
if arr[i] != arr[i - 1]:
arr[write_idx] = arr[i]
write_idx += 1return write_idx
Note O(n) time, O(1) space. The 'read pointer / write pointer' variant is essential for in-place array problems. Always ask: is the input sorted? If yes, two pointers likely applies. If unsorted, consider hash set instead.
Note DISTINCT applies to the entire row when used with multiple columns. SELECT DISTINCT city, state treats (city, state) pairs as the unit of uniqueness.