Sollicitatievraag bij Meta

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

Antwoorden op sollicitatievragen

Anoniem

24 apr 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

Anoniem

8 mei 2012

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

2

Anoniem

25 jul 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

Anoniem

29 aug 2012

I mean combine them

Anoniem

12 mei 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

Anoniem

8 okt 2011

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

Anoniem

28 okt 2011

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

Anoniem

9 dec 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); } }

Anoniem

29 aug 2012

store each characted in hashset and them combine