Remove duplicates from unsorted array

**Problem ** Remove duplicates from unsorted array Example a[1,5,2,6,8,9,1,1,10,3,2,4,1,3,11,3] ```so after that operation the array should look like a[1,5,2,6,8,9,10,3,4,11] Solution **Method 1 - Brute Force - Check every element against every other element** The naive solution is to check every element against every other element. This is wasteful and yields an O(n2) solution, even if you only go "forward". **Method 2 - Sort then remove duplicates** A better solution is sort the array and then check each element to the one next to it to find duplicates. [Read More]

Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplcate characters from the string. Input String : kodeknight Output String : kodenight Solution Method 1 : Brute force void removeDuplicates(char \*str) { if (!str) return; int len = strlen(str); if (len < 2) return; int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) if (strii == strjj) break; if (j == tail) { strtailtail = strii; ++tail; } } strtailtail = '\\0'; } Time Complexity : O(N^2) [Read More]

Remove duplicates from an unsorted linked list

Problem Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? **Example ** Input/Output Original linked list: 2 -> 3 -> 2 -> 5 -> 2 -> 8 -> 2 -> 3 -> 8 -> null After deleting duplicate node: 2 -> 3 -> 5 -> 8 -> null We have already seen deleting duplicates from the sorted list. [Read More]

Duplicate removal in Binary Search Tree

#include <stdio.h\> class BinarySearchTree { private: int data; BinarySearchTree \*left, \*right; public: BinarySearchTree(const int d):data(d), left(NULL), right(NULL){} ~BinarySearchTree() { if(left !\= NULL) { delete left; } if(right !\= NULL) { delete right; } } //remove duplicates void removeDup() { if(left) { left\-\>removeDup(); left\-\>remove(data, this); } if(right) right\-\>removeDup(); } void remove(int value, BinarySearchTree \*parent) { if(value < data && left) { left\-\>remove(value, this); } else if(value \> data && right) { right\-\>remove(value, this); } else remove(parent); } void remove(BinarySearchTree \*parent) //remove this node { if(left \=\= NULL && right \=\= NULL) //leaf { ((parent\-\>left \=\= this) ? [Read More]

Remove duplicates from the sorted array

**Problem **  Remove duplicates from the sorted array Example [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5] -> [1, 2, 3, 4, 5] Method 1- using 3 pointers //using 3 pointers fixDuplicatesInSortedArray(Integer a): start = 0 end = 0 destination = 0 while(start < a.length): while(end < a.length && aendend == astartstart): end++ end-- //copy the distinct element adestinationdestination = astartstart destination++ start = end + 1 end = start //null out the rest while destination < a. [Read More]