code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

041 Different Symbols Naive

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Given a string, find the number of different characters in it.

Example

For s = "cabca", the output should be differentSymbolsNaive(s) = 3.

There are 3 different characters a, b and c.
*/

function differentSymbolsNaive(array) {
  array = array.split('')
  let unqiueChars = [...new Set(array)]
  return unqiueChars.length
}

test('different Symbols Naive', () => {
  expect(differentSymbolsNaive('cabca')).toEqual(3)
});

Created 2019-12-14T23:23:16+05:30 · Edit