A C program to delete duplicate numbers from an integer array without using any predefined functions

Pranay
Sep 25, 2014  ยท  3282 views

Description: This program takes an integer array(with duplicates) as input and outputs an integer array(without duplicates). Here we will go through the logic before coding the actual program. So let us consider an integer array InputArray with below mentioned values.

InputArray - Values as below

1

5

8

4

5

6

9

4

3

4

38

12

In the above input array the numbers 4 and 5 are duplicated. i.e., 4 occur thrice and 5 occur twice. So here, to remove the duplicates we will copy the contents of this array into a new array and while we copy each number, we will omit the duplicate entries.

Algorithm:

  1. Take an element from first array
  2. Before copying into the second array, check if that element is present in the second array.
  3. If it is already present then don't copy. Instead ignore that element and go for next element and follow step 1.
  4. If it is not present then copy that element into the second array and go to step 1.

Code:

for(int i =0, j=0; i<array1.length; i++)
{

    for (int k=0; k<=j; k++)
    {
        if(array1[i]==array2[k])
        {
            isDuplicate = true;
            break;
        } 
    }
    if(isDuplicate)
        continue;
    array2[j++]=array1[i];
}
AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.