Skip to main content


Showing posts from May, 2019

Count occurrences of a character in string in Python

Hello everyone! Today we are going to see how can we count the occurrences of a character in a given string using Python.

Python provides a library method count which can be used to count the occurrence of a substring in a given string. Below code snippet takes a character as a substring and returns the count of that in the source string.
# Enter the string in which we need to search sentence=input("Enter a string\n")# Enter a string/character which needs to be searchedsub_string=input("Enter the substring to search\n")# Getting the count of the sub string in source string - convert both to lowercase if we want to count irrespective of casecount_of_substring=sentence.lower().count(sub_string.lower())# Print the count of the substring in the source stringprint('Count of '+sub_string+' in '+sentence+' is '+str(count_of_substring)) The output of this program will be as follows -
>> Enter a string Help is always given at Hogwarts to those w…

Remove duplicates from an array (JavaScript)

In this post, we will see how to remove duplicates from an array in JavaScript.

We will look into two methods - ES6 and Vanilla JavaScript.
ES6 Method In ES6, this is very simple using the Set constructor and the Spread syntax as below - letarray=[1,3,4,1,2,4,3,4,5,7,6,5,8];letunique=[...newSet(array)];console.log(unique); The output of this code will be -
[1,3,4,2,5,7,6,8]Set - lets you store unique values of any type, whether primitive values or object references.
Spread - allows an iterable to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Using Vanilla JavaScript If you want to use vanilla JavaScript, you can utilize Array.indexOf() and Array.filter() methods as follows - varsandwiches=["turkey","ham","turkey","tuna","pb&j","ham",&q…