Sponsored Content
Top Forums Shell Programming and Scripting Removing duplicate items from an array Post 303007455 by Don Cragun on Thursday 16th of November 2017 09:20:55 PM
Old 11-16-2017
Quote:
Originally Posted by mukulverma2408
Hi Don,

Thanks for your reply, the script is now working perfectly.

Can you please help to explain below part, the script seems to be working fine without decreasing the counter as well.
Code:
((j--))

The script is decreasing the counters.

After removing an element from the array of n items, the arithmetic command ((n--)) decreases the value of n. Note that n-1 is also the upper limit on the outer loop (indexed by i) and n is the upper limit on the inner loop (indexed by j).

When we remove an element from the array (i.e. remove ${array[$j]} by moving higher elements of the array down one position), the ((j--)) forces the next iteration of the inner loop to look at the same element (after moving down higher elements). Without the ((j--)) the first element that was moved down won't be examined to see if it is also a duplicate of the element indexed by the outer loop.

P.S.: Note also that your script compared every element of the array to every element of the array (skipping the cases where it was comparing an item to itself). My script compares element 1 in the array to elements 2 through n, then compares element 2 to elements 3 through n, and so on until it compares element n-1 to element n on the last iteration through the nested loops. Doing it this way eliminates the need to check for comparing an element to itself and gets rid of the duplicated comparisons with pairs of elements that have already been compared.

Last edited by Don Cragun; 11-16-2017 at 10:37 PM.. Reason: Add postscript.
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

removing duplicate lines from a file

Hi, I am trying to remove duplicate lines from a file. For example the contents of example.txt is: this is a test 2342 this is a test 34343 this is a test 43434 and i want to remove the "this is a test" lines only and end up with the numbers in the file, that is, end up with: 2342... (4 Replies)
Discussion started by: ocelot
4 Replies

2. Shell Programming and Scripting

removing items with repeated first 3 character

AWK help: I have a file with following format. I need to remove any entries which are repeated based on first 3 characters. So from the following files I need to remove any entries start with "mas". mas01bct mas02bct mas03bct mas01bct mas01bct mas01bct mas11bct mas01bct mas01bct... (11 Replies)
Discussion started by: amir07
11 Replies

3. Shell Programming and Scripting

removing items from a file with batch

Please assist with awk scirpts: I need to remove items from a file in a batch: The file that I will remove from has the following format: abc00tef:10.81.12.3 abc01tef:10.81.12.3 abc02tef:10.81.12.3 abc03tef:10.81.12.3 abc04tef:10.81.12.3 abc05tef:10.81.12.3 I have a file which... (5 Replies)
Discussion started by: amir07
5 Replies

4. Programming

Removing Items In A ListView

Hi everyone! So I have a listView on my Form named "officeView" I already have the code to add and update info into it, but Im having troubles deleting items out of it. :/ Now I know how to delete an Item from the listView, but I want the item before the deleted item to become automatically... (0 Replies)
Discussion started by: romeo5577
0 Replies

5. Shell Programming and Scripting

Removing Duplicate Variables : SED?

I have a file that needs to index and remove all duplicate variables (keeping the first of the duplicated). Then in another file, from the index I need to find and replace all those duplicated variables we deleted with their primary variable (the one we kept). I think this is a SED question, but if... (13 Replies)
Discussion started by: Blue Solo
13 Replies

6. Shell Programming and Scripting

Add items to ksh array

Hi I need a code help put below items in to array and display in order like this Thanks, Ashan 0AD4 R2 03A1 000198701333 0AD4 R1 270B 000198701616 ------ 0AD4 R2 03A1 000198701333 0AD4 R1 270B 000198701616 (1 Reply)
Discussion started by: ashanabey
1 Replies

7. Shell Programming and Scripting

sed to delete items in an array from a file

I need to create a shell script to delete multiple items (Strings) at a time from a file. I need to iterate through a list of strings. My plan is to create an array and then iterate through the array. My code is not working #!/bin/bash -x declare -a array=(one, two, three, four)... (5 Replies)
Discussion started by: bash_in_my_head
5 Replies

8. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies
COLLATOR_SORT(3)							 1							  COLLATOR_SORT(3)

Collator::sort - Sort array using specified collator

	Object oriented style

SYNOPSIS
public bool Collator::sort (array &$arr, [int $sort_flag]) DESCRIPTION
Procedural style bool collator_sort (Collator $coll, array &$arr, [int $sort_flag]) This function sorts an array according to current locale rules. Equivalent to standard PHP sort(3) . PARAMETERS
o $coll -Collator object. o $arr - Array of strings to sort. o $sort_flag - Optional sorting type, one of the following: o Collator::SORT_REGULAR - compare items normally (don't change types) o Collator::SORT_NUMERIC - compare items numerically o Collator::SORT_STRING - compare items as strings Default sorting type is Collator::SORT_REGULAR. It is also used if an invalid $sort_flag value has been specified. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 collator_sort(3) example <?php $coll = collator_create( 'en_US' ); $arr = array( 'at', 'as', 'as' ); var_export( $arr ); collator_sort( $coll, $arr ); var_export( $arr ); ?> The above example will output: array ( 0 => 'at', 1 => 'as', 2 => 'as', )array ( 0 => 'as', 1 => 'as', 2 => 'at', ) SEE ALSO
Collator constants, collator_asort(3), collator_sort_with_sort_keys(3). PHP Documentation Group COLLATOR_SORT(3)
All times are GMT -4. The time now is 01:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy