Creating array with non-duplicate / unique elements in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating array with non-duplicate / unique elements in ksh
# 1  
Old 05-21-2013
Creating array with non-duplicate / unique elements in ksh

Hi all,
I have created 3 arrays which can have common elements in each like-
arr_a contains str1 str2 str3 str4 str5
arr_b contains str3 str6 str7 str1 str8
arr_c contains str4 str9 str10 str2

each array is created with "set -A arr_name values" command. I want to create a resultant array-say arr_a, which will contain the merging of the above 3 arrays without any duplicate elements like-
arr_a str1 str2 str3 str4 str5 str6 str7 str8 str9 str10

I can merge the array with the same 'set -A arr_a ${arr_a[*]} ${arr_b[*]} ${arr_c[*]}' command, however it contains duplicate elements. Is ther any option i can provide with 'set' to create array with unique elements??

Considering looping is not the right option as the time complexity will directly proportional to the total elements in individual array. So please consider this while providing input.

thanks in advance for the replies.
# 2  
Old 05-21-2013
Here is one way:
Code:
> set -A arr_a str1 str2 str3 str4 str5
> set -A arr_b str3 str6 str7 str1 str8
> set -A arr_c str4 str9 str10 str2
> set -A arr_d $( echo "${arr_a[@]} ${arr_b[@]} ${arr_c[@]}" )
> set -A arr_d $( printf '%s\n' "${arr_d[@]}" | sort -u )
> echo ${arr_d[@]}
str1 str10 str2 str3 str4 str5 str6 str7 str8 str9

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

4. UNIX for Dummies Questions & Answers

Merging tables: identifiying common and unique elements

Hi all, I know how to merge two tables and to remove the duplicated lines based on a field (Column 2) . My next challenge is to be able to identify in a new column those common elements between table A & B, those elements in table A not present in table B and vice versa. A simple count would be... (6 Replies)
Discussion started by: lsantome
6 Replies

5. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

6. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

7. Shell Programming and Scripting

check duplicate elements

hi if i have arry with lots of intergers , how to check if there is duplicate integers in perl ? say I ahve @intergers=( 1,2,3,3,4,5,4,99,99,100) (2 Replies)
Discussion started by: james94538
2 Replies

8. Shell Programming and Scripting

Creating an Array in KSH from output (stdout)

Using Cygwin PDksh - But also have tested it on Linux with same results ---- I have a script that invokes a program/script and returns a string of data (1234 "12 34 56" 6789) and using "set -A" inserting it into an array. script code snipit >> get_array=$(php... (2 Replies)
Discussion started by: carlos25
2 Replies

9. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies

10. Shell Programming and Scripting

creating a dynamic array in ksh

Hi, Is it possible to create a dynamic array in shell script. I am trying to get the list of logfiles that created that day and put it in a dynamic array. I am not sure about it. help me New to scripting Gundu (3 Replies)
Discussion started by: gundu
3 Replies
Login or Register to Ask a Question