manojdandy
BAN USER/**
* return count words that comes once in a string
*
* @param str
* @return
*/
public int countUniqueWOrd(String str) {
Map<String, Integer> words = new HashMap<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == ' ') {
if (words.containsKey(sb.toString())) {
words.put(sb.toString(), words.get(sb.toString()) + 1);
} else {
words.put(sb.toString(), 1);
}
sb.delete(0, sb.toString().length());
} else {
sb.append(str.charAt(i));
}
}
if (words.containsKey(sb.toString())) {
words.put(sb.toString(), words.get(sb.toString()) + 1);
} else {
words.put(sb.toString(), 1);
}
int countUniqueWOrd = 0;
for (String word : words.keySet()) {
if (words.get(word) == 1) {
++countUniqueWOrd;
}
}
return countUniqueWOrd;
}
- manojdandy September 30, 2015public class ReverseGroupSize {
public static void main(String[] args) {
int[] arr3 = { 1, 2, 3, 4, 5, 6, 7, 8 };
// taking group size 3
doReverseInGroupSize(arr3, 3);
for (int ele : arr3) {
System.out.println(ele);
}
}
public static void doReverseInGroupSize(int[] arr, int gsize) {
if (arr == null || arr.length == 0 || gsize == 0 || gsize == 1) {
return;
}
int d = arr.length / gsize;
int rem = arr.length % gsize;
int start = 0;
int end = gsize - 1;
for (int i = 0; i < d; i++) {
reverse(arr, start, end);
start += gsize;
end += gsize;
}
// handling remainder
reverse(arr, start, end + rem - gsize);
}
public static void reverse(int[] arr, int start, int end) {
if (start != end && start <= end) {
swap(arr, start, end);
reverse(arr, start + 1, end - 1);
}
}
public static void swap(int[] arr, int i, int j) {
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
public static int find(int[] numbers) {
int secondMostRepeating = 0;
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
int count = 1;
for (int number : numbers) {
if (mp.containsKey(number)) {
count = mp.get(number);
mp.put(number, ++count);
} else {
mp.put(number, 1);
}
}
int max = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
Set<Entry<Integer, Integer>> set = mp.entrySet();
for (Entry<Integer, Integer> entry : set) {
if (entry.getValue() >= max) {
max = entry.getValue();
} else if (entry.getValue() >= second) {
second = entry.getValue();
secondMostRepeating = entry.getKey();
}
}
return secondMostRepeating;
}
- manojdandy April 03, 2015public static void main(String[] args) {
Map<Integer, Integer> mp = new HashMap<>();
mp.put(0, 0);
mp.put(1, 0);
mp.put(2, 0);
mp.put(3, 0);
mp.put(4, 0);
mp.put(5, 0);
mp.put(6, 0);
mp.put(7, 0);
mp.put(8, 0);
mp.put(9, 0);
String num = "78549203390";
for (char ch : num.toCharArray()) {
int i = Integer.valueOf(Character.toString(ch));
if (mp.containsKey(i)) {
int val = mp.get(i);
mp.put(i, ++val);
}
}
StringBuilder maxNum = new StringBuilder();
for (int key = 9; key >= 0; key--) {
if (mp.containsKey(key)) {
for (int i = 0; i < mp.get(key); i++) {
maxNum.append(key);
}
}
}
System.out.println(maxNum);
}
Its wokring fin9
- manojdandy March 18, 2015
Repnatetouche, Applications Developer at Alcatel Lucent
My name is NateTouche . I currently live in Seattle . One desire that has always been a constant since As a ...
public class CommonAlphabet {
- manojdandy May 07, 2017/**
* @param args
*/
public static void main(String[] args) {
Assert.assertEquals("strng", commonChar("string", "strong"));
Assert.assertEquals(null, commonChar("", "strong"));
}
public static String commonChar(String str, String str1) {
if (str == null || str1 == null || str.isEmpty() || str1.isEmpty()) {
return null;
}
StringBuilder sb = new StringBuilder();
Set<Character> set = new HashSet<>();
for (char ch : str.toCharArray()) {
set.add(ch);
}
for (char ch : str1.toCharArray()) {
if (set.contains(ch)) {
sb.append(ch);
}
}
return sb.toString();
}
}