Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Tuesday, 11 October 2016

Count Number Of Duplicates Elements Present In Array

Count Number Of Duplicates Elements Present In Array


You need to count Number of duplicates elements present in an array that occurs two or more times.

For Example: 

Input: arr = 1,2,3,1,1,2,4,5,2,3
Output :      3  (1 present 3 times, 2 present 3 times , 3 present 2 times )

Count-Duplicates-In-Array
Count-Duplicates-In-Array



#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int arr[] = { 1,2,3,1,1,4,5,6,4,4,5,2,2,12};
        int size1 = sizeof(arr)/sizeof(arr[0]);
int count1 = 0,flag=0, temp = 0;;
int n = 0;

/*first sort array elements so that all duplicates will be next to each other*/

for (int i = 0; i<size1; i++)
{
for (int j = 0; j<size1 - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

       /*Print sorted array*/

for (int i = 0; i<size1; i++)
cout << arr[i] << "  ";
int i = 0;
while(i<size1)
{
flag = 0;
for (int j = i + 1; j<=size1; j++)
{
if (arr[i] == arr[j])
{
n++;
flag++;
if (flag ==1)
count1++;
continue;
/*if elements present the continue to next iteration inside nested loop*/
}
else
{
n++;
break;
}
}
i = n;
}
cout << "\n\n count value:-  " << count1;
getchar();

}



Friday, 7 October 2016

Delete All Node From A Link List Greater Than A Given Value(X)

Delete All Node From Link List Greater Than A Given Value






Linked-List
Linked-List




Program:

void DeleteNode(node** head, int x)
{

    node *ptr = *head;
    node *prev,*temp,*temp1;
    if((*head== NULL))
    {
        printf("\n\nlist is empty");
    }
    else if(ptr->data >x)
    {
        temp = ptr;
        ptr=ptr->next;
        free(temp);
    }
    else
    {
         temp = ptr;
         prev = ptr;
         printf("\n data :  %d",temp->data);
        while(temp!=NULL)
        {
          
            if (temp->data >x)
            {
                temp1 = temp;
                temp=temp->next;
                prev->next = temp;
                free(temp1);
                temp =prev;

            }
            else
            {
                prev=temp;
                temp=temp->next;
            }
        }

    }
}









Sunday, 2 October 2016

Interview Question #1


What happens if you Call a free on a pointer twice?


Deallocating a memory area with free does not make the contents of the pointer NULL. 

Suppose that you have int *a = malloc (sizeof (int)) and a has 0xdeadbeef and you execute free (a) then after execution a still contains 0xdeadbeef but after the free call this memory address is no more reserved for you. 

Something like you have rented a flat with malloc used for some time, returned the flat by free then you might have a duplicate key for the flat, but it is not reserved for you.

Doing a free on an already freed memory will result in double free memory corruption.


Callling-free-twice-on-pointer
Callling-free-twice-on-pointer



Answer Original Source: StackOverFlow

Other Articles:
Kth smallest element , Merge Point In LinkListCompare two String Represented in Link-List


Article Sources For Learning

Other Blogs: Follow here