Skip to content

Instantly share code, notes, and snippets.

View ChathuraGH's full-sized avatar
☺️
coding

Chathura madusanka ChathuraGH

☺️
coding
View GitHub Profile
//Split the string with the spread ... operator instead of .split(''):
'🌯🌯🍣🍻'.split('')
//=> ["\ud83c", "\udf2f", "\ud83c", "\udf2f", "\ud83c", "\udf63", "\ud83c", "\udf7b"]
//vs
[...'🌯🌯🍣🍻']
//=> ["🌯", "🌯", "🍣", "🍻"]
//vs
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));
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);
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');
let str = "atul kumar srivastava";
let obj ={};
for(let s of str)if(!obj[s])obj[s] = 1;else obj[s] = obj[s] + 1;
console.log(obj)
//source
// https://stackoverflow.com/questions/19480916/count-number-of-occurrences-for-each-char-in-a-string
// https://stackoverflow.com/a/60648545/13861187
@ChathuraGH
ChathuraGH / SortToDic_4.js
Created December 1, 2023 19:17
Object.entries(dict).sort
const k = 5;
const dict = {
"x" : 1,
"y" : 6,
"z" : 9,
"a" : 5,
"b" : 7,
"c" : 11,
"d" : 17,
"t" : 3
@ChathuraGH
ChathuraGH / SortToDic_3.js
Created December 1, 2023 19:12
sortObjectByValues to dic
const sortObjectByValues = (dict: { [key: string]: number }, direction: 'asc'| 'desc' = 'asc') => {
return Object.fromEntries(Object.entries(dict).sort((a, b) => {
if (direction === 'asc') {
return a[1] - b[1]
}
return b[1] - a[1]
}))
}
@ChathuraGH
ChathuraGH / SortToDic_2.js
Created December 1, 2023 19:10
dict sortObj
function sortObj(obj) {
// Sort object as list based on values
return Object.keys(obj).map(k => ([k, obj[k]])).sort((a, b) => (b[1] - a[1]))
}
//source
//https://stackoverflow.com/questions/25500316/sort-a-dictionary-by-value-in-javascript
//https://stackoverflow.com/a/74250133/13861187
@ChathuraGH
ChathuraGH / OccCounter_V2.js
Created December 1, 2023 00:42
Character Occurrences Counter of a string
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
@ChathuraGH
ChathuraGH / OccCounter.js
Created December 1, 2023 00:39
Character Occurrences Counter of a string
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}