A C program to delete duplicate numbers from an integer array without using any predefined functions
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:
- Take an element from first array
- Before copying into the second array, check if that element is present in the second array.
- If it is already present then don't copy. Instead ignore that element and go for next element and follow step 1.
- 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];
}