Write a code to remove duplicates in a string.



Write a code to remove duplicates in a string...

Answer / Prince Gupta

One way to remove duplicates from a string is by using an array and sorting:
```c
#include <stdio.h>n#include <string.h>nnint compare(const void *a, const void *b) {n return strcmp(*(const char *)a, *(const char *)b);
}nnvoid removeDuplicates(char str[]) {n int unique_chars[256] = {0}; /* Assuming ASCII */n int unique_count = 0;n int i;

for (i = 0; str[i] != ''; ++i)
if (!unique_chars[str[i]]) {n unique_chars[str[i]] = 1;n unique_count++;n }

printf("Unique characters: ");
for (i = 0; i < 256; ++i)
if (unique_chars[i])
putchar(i);
}nnint main() {n char str[] = "abcdefghijklmnopqrstuvwxyzabcd";
removeDuplicates(str);
printf("n");
return 0;
}n
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What are the primitive data types in c?

1 Answers  


Can we increase size of array in c?

1 Answers  


What is scanf () in c?

1 Answers  


write a function that accepts an integer/char array and an search item.If the search item is there in the array return position of array and value else return -1.without using other array,without sorting,not to use more than one loop?

3 Answers   Mind Tree,


What is file in c preprocessor?

1 Answers  


which one low Priority in c? a)=,b)++,c)==,d)+

10 Answers  


What is the importance of c in your views?

1 Answers  


Is javascript written in c?

1 Answers  


to write a program, that finds the minimum total number of shelves, including the initial one, required for this loading process. The packets are named A, B, C, D, E &#133;&#133;.. Any numbers of packets with these names could be kept in the shelf, as in this example: [ZZLLAAJKRDFDDUUGGYFYYKK]. All packets are to be loaded on cars. The cars are lined in order, so that the packets could be loaded on them. The cars are also named [A, B, C, D, E,&#133;&#133;&#133;&#133;.].

2 Answers   Infosys, TCS,


What are preprocessor directives in c?

1 Answers  


Why c is called top down?

1 Answers  


What is meant by type casting?

1 Answers  


Categories