shobhit657
BAN USER//Iterative using O(nlogn)
public static void pairWithSum(int[] input, int sum) {
Arrays.sort(input); //sorting in increasing order
int l = 0;
int r = input.length - 1;
while (l < r) {
if (input[l] + input[r] == sum) {
// count++;
System.out.println("Pair with given sum " + sum + " is (" + input[l] + ", " + input[r] + ")");
// break;
l++;
r--;
} else if (input[l] + input[r] > sum) {
r--;
} else {
l++;
}
}
}
//using O(n)
private static final int MAX = 100000; // Max size of Hashmap
static void printpairsUsingMap(int arr[], int sum) {
// Declares and initializes the whole array as false
boolean[] map = new boolean[MAX];
for (int i = 0; i < arr.length; ++i) {
int find = sum - arr[i];
if (find >= 0 && map[find]) {
System.out.println("Pair with given sum " + sum + " is (" + arr[i] + ", " + find + ")");
} else {
map[arr[i]] = true;
}
}
}
LinkedHashMap<Character, Integer> charCount = new LinkedHashMap<Character,Integer>();
for(int i=0; i<input.length(); i++){
Character c = input.charAt(i);
if(charCount.containsKey(c)){
charCount.put(c, charCount.get(c)+1);
}
else{
charCount.put(c, 1);
}
}
StringBuilder sb = new StringBuilder();
for(Character c: charCount.keySet()){
sb.append(c);
sb.append(charCount.get(c));
}
return sb.toString();
static void secondMostRepititedNumber(int[] input){
HashMap<Integer, Integer> map = new HashMap<>();
for (int x: input){
if(map.containsKey(x)){
map.put(x, map.get(x) + 1);
}
else{
map.put(x,1);
}
}
List<Integer> temp = new ArrayList<Integer>( map.values());
Collections.sort(temp);
for (int z : map.keySet()){
if(map.get(z) == temp.get(temp.size() - 2)){
System.out.println("Key is " + z + " & its count " + map.get(z));
break;
}
}
}
static void secondMostRepititedNumber(int[] input){
HashMap<Integer, Integer> map = new HashMap<>();
for (int x: input){
if(map.containsKey(x)){
map.put(x, map.get(x) + 1);
}
else{
map.put(x,1);
}
}
List<Integer> temp = new ArrayList<Integer>( map.values());
Collections.sort(temp);
for (int z : map.keySet()){
if(map.get(z) == temp.get(temp.size() - 2)){
System.out.println("Key is " + z + " & its count " + map.get(z));
break;
}
}
}
def wordCount(list1):
for x in range(0,len(list1)):
list1[x] = list1[x].upper()
dict1 = {}
for x in range(0,len(list1)):
if list1[x] in dict1:
dict1[list1[x]].append(x)
else:
dict1[list1[x]] = [x]
for key in dict1:
print key.capitalize(), " - " , len(dict1[key])
how about this? Might be a little big on space complexity but works.
- shobhit657 January 12, 2015
RepCliftonMalone, Android Engineer at ABC TECH SUPPORT
Hello, I am a Seo Analyst with 5 years of experience in helping large ecommerce websites reach higher organic positions ...
+1 for writing it in swift :p
- shobhit657 November 06, 2015