Pergunta de entrevista da empresa Meta

Given a string, remove all the duplicate characters (not necessarily consecutive)

Respostas da entrevista

Sigiloso

24 de abr. de 2011

void removeDuplicates(char *in, char *out) { bool seen[NUM_CHARS] = {false}; while (*p != 0) { if (seen[*p] == 0) { *out++ = *p; } seen[*p] = true; } }

5

Sigiloso

8 de mai. de 2012

each char has ASCII code number, so just XOR all those numbers together, duplicates will eliminate each other.

2

Sigiloso

25 de jul. de 2012

void remove_duplicate(char * str, int len) { bool appeared[NUM_CHARS]; memset(appeared, 0, sizeof(appeared)); int i = 0, j=0; while(i < n) { if(!appeared[str[i]]) { str[j] = str[i]; j ++; appeared[str[i]] = true; } i ++; } str[j] = '\0'; }

2

Sigiloso

29 de ago. de 2012

I mean combine them

Sigiloso

12 de mai. de 2011

public class StringRemove { private final String baseString = "abcdefghijklmnopqrstuvwxyz12345678910"; private final List finalMap = new ArrayList(); private StringBuffer sb = new StringBuffer(); private void findDup(){ for(int i=1 ; i <= baseString.length() ; i++) { String sLocal = baseString.substring(i-1, i); if(finalMap.contains(sLocal)){ finalMap.remove(sLocal); }else{ finalMap.add(sLocal); } } String s = finalMap.toString(); s = s.replace('[',' ').replace(']',' ').replace(',', ' ').replace(',', ' ').trim().replaceAll(" ", ""); System.out.println(s); } public static void main(String s[]){ StringRemove sr = new StringRemove(); sr.findDup(); }

1

Sigiloso

8 de out. de 2011

void RemoveDuplicates(char[] arr, int length) { Map charMap = new Map(); int currenPosition = 0; for (int i=0;i

Sigiloso

28 de out. de 2011

to the last two - just use a boolean array instead of a map or list like Interview Candidate

Sigiloso

9 de dez. de 2011

public class Duplicates { public static String removeDuplicates(String str){ int[] chars = new int[26]; StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length(); i++){ int val = (int)str.charAt(i)-97; if(chars[val]==0){ chars[val]=1; sb.append(str.charAt(i)); } } return sb.toString(); } public static void main(String[] args){ String res = removeDuplicates("faaabook"); System.out.println(res); } }

Sigiloso

29 de ago. de 2012

store each characted in hashset and them combine