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
-Containerwith most water
-Palindrome checking
Variants:-Oppositeends(converging)-Samedirection(fast/slow or lagging)
Example
// JavaScript - Remove duplicates from sorted arrayfunctionremoveDuplicates(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;}
# Python
def remove_duplicates(arr):if not arr:return0
write_idx =1for i inrange(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.
Frequently asked questions
How do you remove duplicates?
This task is covered in 4 stacks on this page: Bash & Linux, Interview Prep, Python, SQL. The "Filter Duplicate Lines" snippet in Bash & Linux uses `uniq [options] [input [output]]`.
Which command does the Bash & Linux example use?
The "Filter Duplicate Lines" snippet uses `uniq [options] [input [output]]`, from the File Content section of the Bash & Linux cheat sheet.
Which stacks cover "remove duplicates" on this page?
Bash & Linux, Interview Prep, Python, SQL. Together they hold 4 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Filter Duplicate Lines": 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.