Delete duplicates from array C++

remove duplicates from array c++
remove duplicates from unsorted array - java
remove duplicates from sorted array javascript
remove duplicates from sorted array ii
remove duplicates from string in c
program to remove duplicate elements in an array in python
remove duplicates from array in c#
c program to merge two arrays and remove duplicates

I am trying to create a simple program which calls on 2 functions. The first function takes a partially filled array, loops through it and deletes any duplicate values. When a value is deleted from the array, the remaining numbers are moved backwards to fill the gap i.e. when the function is finished, all null values of the array will be together at the end.

The second function prints the updated array.

My current code is below. At present when I run my code, the console shows: 2 6 0 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460. It should be showing: 1 2 5 6 if it worked correctly.

Also, I am not sure how to move the remaining elements of the array backwards so that the null values will be together at the end.

#include "pch.h"
#include <iostream>
using namespace std;
void deleteRepeats(int *arr, int arraySize, int& posUsed);
void printArray(int *arr, int arraySize);

int main()
{
int arr[10] = { 1, 2, 2, 5, 6, 1};
int posUsed = 6;
int arraySize = 10;


deleteRepeats(arr, arraySize, posUsed);
printArray(arr, arraySize);

return 0;
}

void deleteRepeats(int *arr, int arraySize, int& posUsed)
{
{
    for (int i = 0; i < arraySize; i++)
    {
        for (int j = i; j < arraySize; j++)
        {
            if (arr[i] == arr[j])
            {
                for (int k = j; k < arraySize; k++)
                {
                    arr[k] = arr[k + 1];

                }
                posUsed--;

            }
            else
                j++;
        }
    }
}
}

void printArray(int *arr, int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
    cout << arr[i] << "  ";
}
}

Given your assignment constraints (more C-like, than idiomatic C++), you can rewrite your function like this, to make it work:

void deleteRepeats(int *arr, int arraySize, int& posUsed)
{
    for (int i = 0; i < posUsed; ++i)
    {
        int duplicates = 0;
        int j = i + 1;
        // find the first duplicate, if exists
        for ( ; j < posUsed; ++j)
        {
            if ( arr[i] == arr[j] ) {
                ++duplicates;
                break;
            }
        }
        // overwrite the duplicated values moving the rest of the elements...
        for (int k = j + 1; k < posUsed; ++k)
        {
            if (arr[i] != arr[k])
            {
                arr[j] = arr[k];
                ++j;
            }
            // ...but skip other duplicates
            else
            {
                ++duplicates;    
            }
        }
        posUsed -= duplicates;
    }
    // clean up (could be limited to the duplicates only)
    for (int i = posUsed; i < arraySize; ++i)
        arr[i] = 0;
}

Java Program to Remove Duplicate Element in an Array, Copy j elements from temp[] to arr[] and return j. C Program to Delete Duplicate Elements from an Array Example 1 This program to remove duplicates from array in c allows the user to enter Array Size and array elements. Next, it is going to find the duplicate elements present in this array, and delete them using For Loop.

It might be easier to imagine the algorithm having separate input and output arrays. Then, in pseudo-code:

for i = 0 to input_array_size-1
    Is input[i] equal to input[j] for any j between 0 and i-1?
    Yes - do nothing
    No - copy input[i] to output

To implement this with shared input and output, you need to have two array sizes, input_array_size and output_array_size. Then, the pseudo-code becomes

output_array_size = 0
for i = 0 to input_array_size-1
    Is array[i] equal to array[j] for any j between 0 and output_array_size-1?
    Yes - do nothing
    No:
        copy array[i] to array[output_array_size]
        Increase output_array_size

Note: it writes output where the input once was, so the check for duplicates should look at all elements that were output. For example, if your array is 1, 2, 1, 3, 5, 6, 3, then for the last 3 the accumulated output is 1, 2, 3, 5, 6, and the code should compare all these with the current element.


To simplify debugging, where it says "do nothing", you can set current element to -1. This way, if you print your array during execution (for debugging), it will be clearer which elements were removed.

Logic to delete duplicate elements from array. Input size and elements in array from user. Store it in some variable say size and arr . To find duplicate elements in given array we need two loops. Run another inner loop to find first duplicate of current element. Inside the inner loop check for duplicate element. Logic to delete duplicate elements from array. Input size and elements in array from user. Store it in some variable say size and arr . To find duplicate elements in given array we need two loops. Run an outer loop from 0 to size . Loop structure must look like for(i=0; i<size; i++ Run another

After deleting duplicate element we get the following array: 1, 6, 2, 9. C programming code. #include <stdio.h>. int main() C program to delete duplicate elements from an array For example, if an array contains following five elements: 1, 6, 2, 1, 9; in this array '1' occurs two times. After deleting duplicate element we get the following array: 1, 6, 2, 9.

Below is a program to find and remove any duplicate element present in the specified array. #include<stdio.h> #include<conio.h> void main() { int a[20], i, j, k, n;  If the element is equal to any other element in the array then shift all the elements to left by one place. The process is repeated till all the elements are compared. C/C++ Program to Remove Duplicate Elements From Array

Given an array of random integers, write an algorithm in C that removes duplicated numbers and return the unique numbers in the original array. E.g Input: {4, 8, 4,  The problem here is to get rid of duplicates and save the unique elements of array into another array with their original sequence. For example : If the input is entered b a c a d t. The result should be : b a c d t in the exact state that the input entered. So, for sorting the array then checking couldn't work since I lost the original sequence.

C Program to Delete Duplicates from an Array : In this article, we will show you, How to write a C Program to Remove Duplicate Elements from an Array. Delete Element from Array; Largest and Smallest Element in Array; Sum of N Numbers using Arrays; Sort Array Elements; Remove Duplicate Elements; Sparse Matrix; Square Matrix; Determinant of 2x2 matrix; Normal and Trace of Square Matrix; Addition and Subtraction of Matrices; Matrix Mulitplication; Pointer. Simple Program; Memory Management; Array of Pointers

Comments
  • arr only has room for 6 elements, but you set arraySize = 10. You can change to int arr[10] = { 1, 2, 2, 5, 6, 1 };
  • Would there be a possibility to use std::vector or std::array?
  • Thanks @JohnnyMopp I have corrected this. Although I have the same problem with console showing no output. Micha, my lecturer does not want us to use vectors, as we have not covered them yet
  • Also, arr[k] = arr[k + 1]; will read past the end of the array when k = arraySize - 1.
  • In your for loops you need to use posUsed and not arraySize.
  • OP states in comments: my lecturer does not want us to use vectors
  • OP cannot use standard library algorithms for that assignment. Also, in your solution the result array will be sorted, which might not be desired. Also, you do not need to use for_each to construct a set from vector.
  • @Ptaq666 good point. I replaced for_each. This enhanced the const correctness.
  • @JohnnyMopp I have not read this comment before posting my answer. Nevertheless, teaching C++ without proper container is ihmo not the right way to do it. There is no point in still using pointers and sizes if a std::vector can be used. C++ should be taught as shown in A Tour of C++ by Stroustrup.
  • Where did the OP state that the algorithm functions couldn't be used? I see that they cannot use vector, but vector is not an algorithm function.