java - Last repeating character in a string -
i searching solution find last repeating character in string says "how doing today", supposed give y
answer. have tried using linked hash map ordered insertions, getting d last repeated one.
string teststring = "how doing today"; char[] teststringarray = teststring.tochararray(); map<character,integer> map = new linkedhashmap<>(); for( char : teststringarray) { if (!(i==' ')) { if(map.containskey(i) ) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } } system.out.println(map); list<entry<character, integer>> list = new arraylist<>(map.entryset()); listiterator<entry<character, integer>> = list.listiterator(list.size()); while(it.hasprevious()) { if(it.previous().getvalue() > 1) system.out.println(it.previous().getkey()); } }
help appreciated.
this code scans last character , checks if it's present in remaining substring before first character:
string str = "how doing today"; int len = str.length(); int = len - 1; (; >= 1; i--) { if (str.substring(0, i).contains(str.charat(i) + "")) { break; } } if (i == 0) { system.out.println("no repeating character"); } else system.out.println(str.charat(i));
Comments
Post a Comment