Finding unique elements in an string array in C

后端 未结 5 2156
轻奢々
轻奢々 2021-01-05 16:39

C bothers me with its handling of strings. I have a pseudocode like this in my mind:

char *data[20]; 

char *tmp; int i,j;

for(i=0;i<20;i++) {
  tmp = da         


        
5条回答
  •  隐瞒了意图╮
    2021-01-05 17:08

    Think a bit more about your problem -- what you really want to do is look at the PREVIOUS strings to see if you've already seen it. So, for each string n, compare it to strings 0 through n-1.

    print element 0 (it is unique)
    for i = 1 to n
      unique = 1
      for j = 0 to i-1 (compare this element to the ones preceding it)
        if element[i] == element[j]
           unique = 0
           break from loop
      if unique, print element i
    

提交回复
热议问题