CREATE OWN LIBRARY

Remove Duplicate Elements from an Array

Back to Programming

Description

The program is written here to remove the duplicate elements from an array and then rearrange the elements of the array and display it. For rearranging any sorting algorithm can be used but for the simplicity of the program bubble sort technique is used.

For example, let the array be:

12

15

52

12

34

52

36

          0                          1                         2                           3                          4                           5                          6            

After removing the duplicate elements the array will be:

12

15

52

34

36

                0                                      1                                     2                                      3                                     4

After removing duplicate elements the array will be sorted and the final array will be:

12

15

34

36

52

Algorithm

INPUT: The array elements

OUTPUT: Array elements after sorting and rearranging

PROCESS:

Step 1: [Taking the inputs]

               Read n [number of elements]

               For i=0 to n-1 repeat

                              Read a[i]

               [End of ‘for’ loop]

Step 2: [Function to remove duplicate elements]

For i = 0 to n-1 repeat

                              For j = i+1 to n-1 repeat

                                             If a[j] = a[i] then

                                                           For k = j to n-1 repeat

                                                                          Set a[k] <- a[k+1]

                                                           [End of ‘for’ loop]

                                                           Set n<-n-1

                                             [End of ‘if’]

                                             Else

                                                           Set  j<-j+1

                                             [End of ‘else’]

                              [End of ‘for’ loop]

               [End of ‘for’ loop]

               [Rearranging the array elements]

               For i=0 to n-1 repeat

                              For j=0 to n-i-2 repeat

                                             If a[j]>a[j+1] then

                                                            Set t<-a[j]

                                                            Set a[j]<-a[j+1]

                                                            Set a[j+1]<-t

                                             [End of ‘if’]

                              [End of ‘for’ loop]

               [End of ‘for’ loop]

               [Displaying the array after removing the duplicates]

               Print "The array after removing the duplicates and rearranging is: "

               For i = 0 to n-1 repeat

                              Print  a[i]

               [End of ‘for’ loop]

Step 3: Stop.

Code

TIME COMPLEXITY:

The time complexity of this program is O(n3) where ‘n’ is the number of elements of the array.

SPACE COMPLEXITY:

The space complexity of this program O(n) where ‘n’ is the number of elements of the array