Bash Script to Remove Duplicates from Array
declare -A seen; for i in "${arr[@]}"; do [[ -z ${seen[$i]} ]] && echo "$i" && seen[$i]=1; done.Examples
How to Think About It
Algorithm
Code
#!/bin/bash arr=(apple banana apple orange banana) declare -A seen for item in "${arr[@]}"; do if [[ -z ${seen[$item]} ]]; then echo "$item" seen[$item]=1 fi done
Dry Run
Let's trace the array (apple banana apple orange banana) through the code
Initialize associative array
seen is empty
Process first element 'apple'
seen[apple] is empty, print 'apple', set seen[apple]=1
Process second element 'banana'
seen[banana] is empty, print 'banana', set seen[banana]=1
Process third element 'apple'
seen[apple] is 1, skip printing
Process fourth element 'orange'
seen[orange] is empty, print 'orange', set seen[orange]=1
Process fifth element 'banana'
seen[banana] is 1, skip printing
| Element | Seen Before? | Action |
|---|---|---|
| apple | No | Print apple, mark seen |
| banana | No | Print banana, mark seen |
| apple | Yes | Skip |
| orange | No | Print orange, mark seen |
| banana | Yes | Skip |
Why This Works
Step 1: Use associative array for tracking
The declare -A seen creates a map where keys are array elements and values mark if seen.
Step 2: Check each element once
For each element, the script checks if it is already in seen to avoid duplicates.
Step 3: Print only unique elements
Only elements not marked as seen are printed and then marked to prevent future duplicates.
Alternative Approaches
arr=(apple banana apple orange banana) printf "%s\n" "${arr[@]}" | sort | uniq
arr=(apple banana apple orange banana) for i in "${arr[@]}"; do if ! grep -qx "$i" temp.txt 2>/dev/null; then echo "$i" echo "$i" >> temp.txt fi done rm -f temp.txt
Complexity: O(n) time, O(n) space
Time Complexity
The script loops once through all elements, checking and inserting in an associative array in constant time, so it runs in O(n).
Space Complexity
It uses extra space proportional to the number of unique elements to store them in the associative array, so O(n).
Which Approach is Fastest?
Using an associative array is fastest and preserves order, while sorting methods are slower and reorder elements.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Associative array | O(n) | O(n) | Preserving order, fast lookup |
| Sort and uniq | O(n log n) | O(n) | Simple scripts, order not important |
| File and grep | O(n^2) | O(n) | Very basic Bash without associative arrays |