In this article we will provide you the bash script code to remove duplicate elements from array. In this approach, the order of list items may change.
Code Example –
#!/bin/bash
remove_array_duplicates() {
declare -A tmp_array
for i in "[email protected]"; do
[[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
done
printf '%s\n' "${!tmp_array[@]}"
}
arr=(we love nature we love animals)
remove_array_duplicates "${arr[@]}"
# Output:
# nature
# love
# we
# animals