This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is gist. | |
There are many like it, but this one is mine. | |
It is my life. | |
I must master it as I must master my life. | |
Without me gist is useless. | |
Without gist, I am useless. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dict = { | |
"x": 1, | |
"y": 6, | |
"z": 9, | |
"a": 5, | |
"b": 7, | |
"c": 11, | |
"d": 17, | |
"t": 3 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sort_object(obj) { | |
items = Object.keys(obj).map(function(key) { | |
return [key, obj[key]]; | |
}); | |
items.sort(function(first, second) { | |
return second[1] - first[1]; | |
}); | |
sorted_obj={} | |
$.each(items, function(k, v) { | |
use_key = v[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DictUtils { | |
static entries(dictionary) { | |
try { | |
//ECMAScript 2017 and higher, better performance if support | |
return Object.entries(dictionary); | |
} catch (error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s = 'hello'; | |
var result = [...s].reduce((a, e) => { a[e] = a[e] ? a[e] + 1 : 1; return a }, {}); | |
console.log(result); // {h: 1, e: 1, l: 2, o: 1} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function withAMap(str) { | |
// A map for the character=>count mappings | |
const counts = new Map(); | |
// Loop through the string... | |
for (const ch of str) { | |
// Get the count for it, if we have one; we'll get `undefined` if we don't | |
// know this character yet. Using nullish coalescing (`??`), we can turn | |
// that `undefined` into a `0`. (In obsolete environments that don't |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Split the string with the spread ... operator instead of .split(''): | |
'🌯🌯🍣🍻'.split('') | |
//=> ["\ud83c", "\udf2f", "\ud83c", "\udf2f", "\ud83c", "\udf63", "\ud83c", "\udf7b"] | |
//vs | |
[...'🌯🌯🍣🍻'] | |
//=> ["🌯", "🌯", "🍣", "🍻"] | |
//vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Char_Count(str1) { | |
var chars = {}; | |
str1.replace(/\S/g, function(l){chars[l] = (isNaN(chars[l]) ? 1 : chars[l] + 1);}); | |
return chars; | |
} | |
var myString = "This is my String"; | |
console.log(Char_Count(myString)); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countChrOccurence ('hello') { | |
let charMap = new Map(); | |
const count = 0; | |
for (const key of str) { | |
charMap.set(key,count); // initialize every character with 0. this would make charMap to be 'h'=> 0, 'e' => 0, 'l' => 0, | |
} | |
for (const key of str) { | |
let count = charMap.get(key); | |
charMap.set(key, count + 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countChar(str) { | |
let myObj= {}; | |
for (let s of str) { | |
if ( myObj[s] ? myObj[s].count ++ : myObj[s] = { count : 1 } ); | |
} | |
return myObj; | |
} | |
var charCount = countChar('abcceddd'); |
OlderNewer