How to find duplicate in an array with using a single array - arrays

I came across a code which is able to find the duplicate in the array using tht same particular array, but however it doesnot work well when the default array contains negative value
void printRepeating(int arr[], int size)
{
int i;
printf("\n The repeating elements are");
for(i = 0; i < size; i++)
{
if(arr[abs(arr[i])] > 0)
arr[abs(arr[i])] = -arr[abs(arr[i])];
else
printf(" %d ", abs(arr[i]));
}
}
int main()
{
int arr[] = {1, 3, 2, 2, 1};
int arr_size = sizeof(arr)/sizeof(arr[0]);
printRepeating(arr, arr_size);
getchar();
return 0;
}
This is the code I found which works fine for positive values but not for negative values.any suggestions would be great

I wonder how is this working fine. Look at the code. You have a printRepeating function, which expects an array, representing the set of elements and an integer, representing the size of the set of elements. So far, so good
In this function you are using the element values as indexes. That's not how things should be done. If those elements are negative, then they are out of bounds. This is what you correctly observed. Also, if those elements are greater or equal than size, then you attempt to use elements outside the array. This is the syntactic level of the problem.
Let's see the semantics. Let us suppose that we will have no out of bounds problem even with big positive numbers or negatives. Let us focus at the idea of the algorithm. It negates the sign of the given index and at the next occurrence negates again. It is not elegant to change the values of an array when that is not the purpose. There are situations, when changing element values is not an option, so you should only change them when that is what you intend. Let's see whether you like this one:
void printRepeating(int arr[], int size)
{
int i;
int j;
printf("\n The repeating elements are");
for(i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr[i] == arr[j]) {
//handle the duplicate
j = size;
}
}
}
}
EDIT:
Based on the newly described needs, you need two functions:
- isDuplicate
- insertElement
int isDuplicate(int arr[], int size, int newElement) {
int i;
for (i = 0; i < size; i++) {
if (arr[i] == newElement) {
return 1;
}
}
return 0;
}
int insertElement(int arr[], int size, int newElement) {
int dupe = isDuplicate(arr, size, newElement);
if (dupe == false) {
arr[size] = newElement;
}
return dupe;
}

Related

How to fix this parameters in function max_2d? conversion from const int to int is the error

I'm trying to find the biggest number in a multidimensional array, but Visual Studio has a problem compiling the code successfully.
It has a problem with conversion from int to const int * when I use parameter size as a limitation of for loop. It is not able to compare int i from for loop with parameter size. Can anyone help?
Here is the error list: https://imgur.com/a/HcNqgmx
int max_2d(const int size[], int array[][size]) {
if (array == NULL) return -1;
int max = array[0][0];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (max < array[i][j]) max = array[i][j];
}
}
return max;
}
int main() {
int array[2][2] = { {1,2},{0,-3} };
printf("%d\n", max_2d(2, array));
return 0;
}
In this state i expect an output return max (where max is the biggest number) = 2.
The first parameter of the function const int size, should be a number not an array, so it should be declare like const int size.
Also, this code will work, but its logically incorrect as second index of array (arr[][])
should be known, but here you are passing it as arr[][size].
It is a static memory allocation. The compiler should know the size of all the variables declared at compilation time.

add and remove elements from an array

i m working on this small C program to fill an array, insert and remove elements from an array and finally printing that array.
The program consists of a main() function, the addToArray() function,
remFromArray() function and the printArray() function.
here's my main method:
which prompts the user for a sequence of positive integers
• for each integer entered by the user, call the addToArray function to insert the integer into the array
• call the printArray function to print out the content of the array.
NOTE:do not prompt the user for the number of integers to be entered; you must accept input until the user enters a negative number
#include <stdio.h>
#define maxSize 100 //maxSize for array
int addToArray(int* arr, int size, int numToAdd);
int remFromArray(int* arr, int size, int numToGo);
void printArray(int* arr, int size);
int main (void){
int arr[maxSize];
int i, j;
printf("Enter a positive integer to add to an array\n");
while (i >= 0 ){
scanf("%d",&i);
if (i >= 0 ) {
addToArray(arr, maxSize, i);
printf("Enter another positive integer for array\n");
}
else{
printArray(arr, maxSize);
}
}
}
these are my other function that i created:
addToArray() =>
/*find the correct index in the array where to insert the
new element, so array in ascending order
- make room for the new element by moving other elements
- store the new element at the correct index
- return the new size (number of elements) of the array */
int addToArray(int* arr, int size, int numToAdd){
int i, n, m, pos;
for (int i = 0; i < maxSize; i++)
{
if (numToAdd < arr[i])
{
pos = i;
break;
}
if (numToAdd > arr[n-1])
{
pos = maxSize;
break;
}
}
if (pos != maxSize)
{
m = maxSize - pos + 1 ;
for (int i = 0; i <= m; i++)
{
arr[maxSize - i + 2] = arr[maxSize - i + 1] ;
}
}
arr[pos] = numToAdd;
}
void printArray(int* arr, int size){
printf("Resultant array is\n");
for (int c = 0; c <= maxSize; c++){
printf("%d\n", arr[c]);
}
}
as you can see I've tried writing the main(), addToArray(), printArray(), but for some reason its not working right. my printArray() is not displaying the desired result.
I'll be glad if you could look at this code and guide me through it.
thanks a million
First, you don't set values to the variables you create. For exemple, all of those int i, n, m, pos; have indeterminate values, not 0 as you might expect. Don't forget to asign them a value before using to avoid bad surprises.
Second, be carefull about indexes of your array. For example, arr[maxSize - i + 2], when i == 0, then the index will be maxSize + 2, witch is invalid position and can lead to segmentation fault.

How to delete multiple elements from a array at the same time

I want to delete multiple elements from array using index array,this is my code:
// b is an index array, n is size of b,
// player is the array need to be delete elements,
// size is the size of player
void play_cards(int b[],int n,int player[],int *size){
int i;
for(i = 0; i < n; i++)
delete_cards(b[i],player,size);
}
void delete_cards(int n,int player[],int *size){
int i;
for(i = n; i < *size; i++)
player[i] = player[i+1];
*size -= 1;
}
int main(void){
int player[10] = {1,2,3,3,4,4,5,5,6,7};
int index[6] = {2,3,4,5,6,7};
int size = 10;
play_cards(index,6,player,&size);
for(int i = 0; i < size; i++)
printf("%d|",player[i]);
puts("");
return 0;
}
I expect print the player should be 1,2,6,7 instead of 1,2,3,4. How should I fix it?
First I would not call the function delete_cards as it suggests that it deletes multiple cards which it does not - just delete_card would make things more clear.
Anyway - when you change the player array before you have played all cards in the index array, you change the meaning of the indexes. This is why your current code doesn't work.
So you can do two things:
a) Play all cards first and then delete the cards played. This could be done by first marking played card with -1 and then have a loop where you removed all element being -1
or
b) Play a card, delete it and adjust the remaining elements in index by decrementing them by one. Note: This solution requires that index is sorted (lowest first).
Solution a) could look something like this:
void delete_played_cards(int player[],int *size)
{
int i;
int next_pos = 0;
int deleted = 0;
for(i = 0; i < *size; i++)
{
if (player[i] != -1)
{
player[next_pos] = player[i];
if (i != next_pos)
{
player[i] = -1;
}
++next_pos;
}
else
{
++deleted;
}
}
*size -= deleted;
}
void play_cards(int b[],int n,int player[],int *size)
{
int i;
for(i = 0; i < n; i++)
{
player[b[i]] = -1; // Mark card as played
}
delete_played_cards(player,size);
}
int main(void)
{
int player[10] = {1,2,3,3,4,4,5,5,6,7};
int index[6] = {2,3,4,5,6,7};
int size = 10;
play_cards(index,6,player,&size);
for(int i = 0; i < size; i++)
printf("%d|",player[i]);
puts("");
return 0;
}
Modify play_cards:
void play_cards(int b[], int n, int player[], int *size)
{
int i;
for(i = n-1; i >= 0; i--)
delete_cards(b[i],player,size);
}
This will start deleting from the end of array.
As BLUEPIXY mentioned.
here is a pseudocode that you can work with:
given a sorted list, 1..n
for i = 2 up to length of list:
if list[i] is equal to list[i-1]:
shift the sublist [2..] 1 position to the left
else
increment i by 1
If you want to delete easily and efficiently without using loop you can use memcpy
#include <stdio.h>
#include <string.h>
#define INDEX_MAX 6
int main ()
{
int size = 10;
int src[] = {1,2,3,3,4,4,5,5,6,7};
int index[] = {2,3,4,5,6,7};
int x;
size = size - INDEX_MAX;
memcpy(src+2, src+8, sizeof(int)*(size-2));// - 2 since index 1 and 2 remains in the array
for(x = 0; x < size; x++){
printf("%d",src[x]);
}
return(0);
}

How do I write a function that returns true if every integer in the array is different?

I have to write a function that will return true if every integer in the array is unique (different). So, I've tried correcting my for loops/my if statement and I've tried running the tests that I wrote. But, the test for a string with an integer appearing more than once still fails. I've reviewed my code but I still can't find the problem.
#include "in.h"
int in(int input[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
{
if (input[i] == input[j])
{
return 0;
}
}
}
return 1;
}
Here are my test cases:
#include "in.h"
#include "checkit.h"
void in_tests(void)
{
int input[3] = {2, 4, 5};
int answer;
answer = in(input, 3);
checkit_int(answer, 1);
int input1[4] = {1, 3, 4, 1};
int answer1;
answer1 = in(input1, 4);
checkit_int(answer, 0);
}
int main()
{
in_tests();
return 0;
}
without sort, an O(n2):
j begins with i+1.
int IsDiff(int array[], int count)
{
int i,j;
for (i=0; i<count; i++)
{
for (j=i+1;j<count;j++)
{
if (array[i] == array[j])
return 0;
}
}
return 1;
}
If space is not an issue, then we can have a O(n) solution using a hashtable. Start storing each element of the array in a hashtable. While inserting elements, make a check to see if it already present in the hashtable ( which takes O(1) time.) If the element is already present, then return false immediately, else iterate until the end of array and return true.
actually, thats not why your function doesn't work. the main reason is because currently you are checking if any pair DONT match, which would be great if you wanted to see if all the elements matched, but you want to do the opposite, so you want to check the opposite, if any pair DOES match. so first change your if to be
if(input[i] == input[j]) return false;
if one pair is equal then you know that your test has already failed so there is no need to check the remaining pairs, just return false there and then.
the only other thing to do is to sort out your loops so that you only iterate over each pair once and don't compare a value against it's self. to do that change the for loops to be:
for(int i =0; i<size-1; i++)
for(int j=i+1; j<size; j++)
then if you make it to the end of the function, it means no pair as matched, so just return true;
O(n^2) as well.. but I added this function that makes the code more understandable...
int unique(int input[], int value,int size){
int times=0;
for (int i = 0; i < size; i++)
{
if(input[i]==value){
times++;
if(times==2)
return 0;
}
}
return 1;
}
int in(int input[], int size)
{
int x, answer;
for (int i = 0; i < size; i++)
{
if(!unique(input,input[i],size))
return 0;
}
return 1;
}
You are very close, try this:
#include "in.h"
int in(int input[], int size)
{
int answer = 1;
for (int i = 0; i < size-1; i++)
{
for (int j = i+1; j < size; j++) //check everything above your current
//value because you have already checked
//below it.
{
if (input[i] == input[j])
{
answer = 0; //If any are not unique you
break; //should exit immediately to save time
}
}
if (answer == 0)
break
}
return answer;
}
Thinking about the problem
let's say you have an array like this: int array[] = { 3, 1, 4, 8, 2, 6, 7, 7, 9, 6 };
if you start with the first element you need to compare to the rest of the elements in the set ; so 3 needs to be compared to 1, 4, 3... etc.
if 3 is unique you need to now go to 1 and look at the remaining elements, but next time around you don't need to worry about the 3 ; so your value under consideration (let's call that V) needs to be each of, array[0] to array[N-1] where N is the size of array (in our case 10);
But if we imagine ourselves in the middle of the array, we'll notice that we have already compared previous elements to the current element when the previous element was the value under consideration. So we can ignore any elements "behind" us; which means comparison has to start the each time with the INDEX of current value under consideration (let's call that k) and compare it to values starting with the NEXT index and going to the end of the array ; so pseudo-code for our array of 10 it would look like this:
int is_a_set(int array[])
{
for ( k = 0; k < 10-1 ; k++ ) { /* value under consideration */
v = array[k];
for ( m = k+1 ; m < 10; m++ ) { /* compared to the remaining array starting
* starting from the next element */
if ( v == array[m] ) { /* we found a value that matches, no need to
* to continue going, we can return from this function
*/
return true;
}
}
}
return false;
}
now you can use something like:
if ( is_a_set(my_array) ) {
}
You should be able to use this to figure the rest of your code out :-)
NOTE: a collection with unique elements is called a set so I named the function is_a_set
HINTS: convert size of the array (10) into a parameter or a variable of some sort
Here's my attempt at it (very simple and inefficient though):
#include <stdio.h>
int uniq_int(int arr [], int size)
{
int i, j;
for (i = 0; i < size; i++)
for (j = i + 1; j < size; j++)
if (arr[i] == arr[j])
return 0;
return 1;
}
int main()
{
int arr [] = {1, 2, 4, 3, 5};
(uniq_int(arr, 5)) ? printf("Unique!\n") : printf("Not...\n");
/* the above can be written more traditionally like this:
if (uniq_int(arr, 5) != 0)
{
printf("Unique!\n");
}
else
{
printf("Not...\n");
}
*/
return 0;
}
This will compare every member of the array against the rest, starting from the first
and comparing it against the next one and so on until the end of the inner loop.
Then we start the outer loop again but this time we compare the second element of the array against the ones after it.
As soon as a match is found the function will return and there won't be a need to compare the other elements. Otherwise the outer loop will go on until the last element and once that's reached it will exit and return 1, meaning every member is unique.
It has a complexity of O(n^2) as the set is processed twice for each member in the worst case.

Removing Duplicates from an Array using C [duplicate]

This question already has answers here:
Algorithm: efficient way to remove duplicate integers from an array
(34 answers)
Closed 8 years ago.
I want small clarification in array concept in C.
I have array:
int a[11]={1,2,3,4,5,11,11,11,11,16,16};
I want result like this:
{1,2,3,4,5,11,16}
Means I want remove duplicates.
How is it possible?
You can't readily resize arrays in C - at least, not arrays as you've declared that one. Clearly, if the data is in sorted order, it is straight-forward to copy the data to the front of the allocated array and treat it as if it was of the correct smaller size (and it is a linear O(n) algorithm). If the data is not sorted, it gets messier; the trivial algorithm is quadratic, so maybe a sort (O(N lg N)) followed by the linear algorithm is best for that.
You can use dynamically allocated memory to manage arrays. That may be beyond where you've reached in your studies, though.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int intcmp(const void *pa, const void *pb)
{
int a = *(int *)pa;
int b = *(int *)pb;
if (a > b)
return +1;
else if (a < b)
return -1;
else
return 0;
}
static int compact(int *array, int size)
{
int i;
int last = 0;
assert(size >= 0);
if (size <= 0)
return size;
for (i = 1; i < size; i++)
{
if (array[i] != array[last])
array[++last] = array[i];
}
return(last + 1);
}
static void print(int *array, int size, const char *tag, const char *name)
{
int i;
printf("%s\n", tag);
for (i = 0; i < size; i++)
printf("%s[%d] = %d\n", name, i, array[i]);
}
int main(void)
{
int a[11] = {1,2,3,4,5,11,11,11,11,16,16};
int a_size = sizeof(a) / sizeof(a[0]);
print(a, a_size, "Before", "a");
a_size = compact(a, a_size);
print(a, a_size, "After", "a");
int b[11] = {11,1,11,3,16,2,5,11,4,11,16};
int b_size = sizeof(b) / sizeof(b[0]);
print(b, b_size, "Before", "b");
qsort(b, b_size, sizeof(b[0]), intcmp);
print(b, b_size, "Sorted", "b");
b_size = compact(b, b_size);
print(b, b_size, "After", "b");
return 0;
}
#define arraysize(x) (sizeof(x) / sizeof(x[0])) // put this before main
int main() {
bool duplicate = false;
int a[11] = {1,2,3,4,5,11,11,11,11,16,16}; // doesnt have to be sorted
int b[11];
int index = 0;
for(int i = 0; i < arraysize(a); i++) { // looping through the main array
for(int j = 0; j < index; j++) { // looping through the target array where we know we have data. if we haven't found anything yet, this wont loop
if(a[i] == b[j]) { // if the target array contains the object, no need to continue further.
duplicate = true;
break; // break from this loop
}
}
if(!duplicate) { // if our value wasn't found in 'b' we will add this non-dublicate at index
b[index] = a[i];
index++;
}
duplicate = false; // restart
}
// optional
int c[index]; // index will be the number of objects we have in b
for(int k = 0; k < index; k++) {
c[k] = b[k];
}
}
If you really have to you can create a new array where that is the correct size and copy this into it.
As you can see, C is a very basic (but powerful) language and if you can, use a vector to but your objects in instead (c++'s std::vector perhaps) which can easily increase with your needs.
But as long as you only use small numbers of integers you shouldn't loose to much. If you have big numbers of data, you can always allocate the array on the heap with "malloc()" and pick a smaller size (maybe half the size of the original source array) that you then can increase (using realloc()) as you add more objects to it. There is some downsides reallocating the memory all the time as well but it is a decision you have to make - fast but allocation more data then you need? or slower and having the exact number of elements you need allocated (which you really cant control since malloc() might allocate more data then you need in some cases).
//gcc -Wall q2.cc -o q2 && q2
//Write a program to remove duplicates from a sorted array.
/*
The basic idea of our algorithm is to compare 2 adjacent values and determine if they
are the same. If they are not the same and we weren't already looking previusly at adjacent pairs
that were the same, then we output the value at the current index. The algorithm does everything
in-place and doesn't allocate any new memory. It outputs the unique values into the input array.
*/
#include <stdio.h>
#include <assert.h>
int remove_dups(int *arr, int n)
{
int idx = 0, odx = -1;
bool dup = false;
while (idx < n)
{
if (arr[idx] != arr[idx+1])
{
if (dup)
dup = false;
else
{
arr[++odx] = arr[idx];
}
} else
dup = true;
idx++;
}
return (odx == -1) ? -1 : ++odx;
}
int main(int argc, char *argv[])
{
int a[] = {31,44,44,67,67,99,99,100,101};
int k = remove_dups(a,9);
assert(k == 3);
for (int i = 0;i<k;i++)
printf("%d ",a[i]);
printf("\n\n");
int b[] = {-5,-3,-2,-2,-2,-2,1,3,5,5,18,18};
k = remove_dups(b,12);
assert(k == 4);
for (int i = 0;i<k;i++)
printf("%d ",b[i]);
printf("\n\n");
int c[] = {1,2,3,4,5,6,7,8,9};
k = remove_dups(c,9);
assert(k == 9);
for (int i = 0;i<k;i++)
printf("%d ",c[i]);
return 0;
}
you should create a new array and you should check the array if contains the element you want to insert before insert new element to it.
The question is not clear. Though, if you are trying to remove duplicates, you can use nested 'for' loops and remove all those values which occur more than once.
C does not have a built in data type that supports what you want -- you would need to create your own.
int a[11]={1,2,3,4,5,11,11,11,11,16,16};
As this array is sorted array, you can achieve very easily by following code.
int LengthofArray = 11;
//First elemnt can not be a duplicate so exclude the same and start from i = 1 than 0.
for(int i = 1; i < LengthofArray; i++);
{
if(a[i] == a[i-1])
RemoveArrayElementatIndex(i);
}
//function is used to remove the elements in the same as index passed to remove.
RemoveArrayElementatIndex(int i)
{
int k = 0;
if(i <=0)
return;
k = i;
int j =1; // variable is used to next item(offset) in the array from k.
//Move the next items to the array
//if its last item then the length of the array is updated directly, eg. incase i = 10.
while((k+j) < LengthofArray)
{
if(a[k] == a[k+j])
{
//increment only j , as another duplicate in this array
j = j +1 ;
}
else
{
a[k] = a[k+j];
//increment only k , as offset remains same
k = k + 1;
}
}
//set the new length of the array .
LengthofArray = k;
}
You could utilise qsort from stdlib.h to ensure your array is sorted into ascending order to remove the need for a nested loop.
Note that qsort requires a pointer to a function (int_cmp in this instance), i've included it below.
This function, int_array_unique returns the duplicate free array 'in-place' i.e. it overwrites the original and returns the length of the duplicate free array via the pn pointer
/**
* Return unique version of int array (duplicates removed)
*/
int int_array_unique(int *array, size_t *pn)
{
size_t n = *pn;
/* return err code 1 if a zero length array is passed in */
if (n == 0) return 1;
int i;
/* count the no. of unique array values */
int c=0;
/* sort input array so any duplicate values will be positioned next to each
* other */
qsort(array, n, sizeof(int), int_cmp);
/* size of the unique array is unknown at this point, but the output array
* can be no larger than the input array. Note, the correct length of the
* data is returned via pn */
int *tmp_array = calloc(n, sizeof(int));
tmp_array[c] = array[0];
c++;
for (i=1; i<n; i++) {
/* true if consecutive values are not equal */
if ( array[i] != array[i-1]) {
tmp_array[c] = array[i];
c++;
}
}
memmove(array, tmp_array, n*sizeof(int));
free(tmp_array);
/* set return parameter to length of data (e.g. no. of valid integers not
* actual allocated array length) of the uniqe array */
*pn = c;
return 0;
}
/* qsort int comparison function */
int int_cmp(const void *a, const void *b)
{
const int *ia = (const int *)a; // casting pointer types
const int *ib = (const int *)b;
/* integer comparison: returns negative if b > a
and positive if a > b */
return *ia - *ib;
}
Store the array element with small condition into new array
**just run once 100% will work
!)store the first value into array
II)store the another element check with before stored value..
III)if it exists leave the element--and check next one and store
here the below code run this u will understand better
int main()
{
int a[10],b[10],i,n,j=0,pos=0;
printf("\n enter a n value ");
scanf("%d",&n);
printf("\n enter a array value");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);//gets the arry value
}
for(i=0;i<n;i++)
{
if(check(a[i],pos,b)==0)//checks array each value its exits or not
{
b[j]=a[i];
j++;
pos++;//count the size of new storing element
}
}
printf("\n after updating array");
for(j=0;j<pos;j++)
{
printf("\n %d",b[j]);
} return 0;
}
int check(int x,int pos,int b[])
{ int m=0,i;
for(i=0;i<pos;i++)//checking the already only stored element
{
if(b[i]==x)
{
m++; //already exists increment the m value
}
}
return m;
}

Resources