Skip to content

Instantly share code, notes, and snippets.

View mustafadalga's full-sized avatar

Mustafa Dalga mustafadalga

View GitHub Profile
@mustafadalga
mustafadalga / charCount.js
Last active February 4, 2021 14:33
A function that takes a string and returns the count of each character in the string
function charCount(str) {
if (typeof str != "string") return;
str = str.toLowerCase()
const result = {}
str.split("").forEach(char => {
if ((/[a-z0-9]/).test(char)) {
result[char]=result.hasOwnProperty(char) ? ++result[char] : 1
}
});
return result;
@mustafadalga
mustafadalga / validateAnagram.js
Created February 4, 2021 21:00
Given two strings, write a function to determine if the second string is an anagram of the first. An anagram is a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
function validAnagram(str1, str2) {
if (typeof str1 != "string" || typeof str2 != "string") return false;
if (str1.length != str2.length) return false;
[str1, str2] = [str1.toLocaleLowerCase(), str2.toLocaleLowerCase()]
let frequencyStr1 = {}
let frequencyStr2 = {}
for (let char of str1) {
frequencyStr1[char] = (frequencyStr1[char] || 0) + 1
}
for (let char of str2) {
@mustafadalga
mustafadalga / factorial.js
Created February 7, 2021 12:39
A method that takes a number and calculates the factorial of the number
function factorial(num) {
if (num === 1) return 1;
return num * factorial(num - 1);
}
//factorial(5) 120
@mustafadalga
mustafadalga / linearSearch.js
Last active February 14, 2021 09:16
Linear Search Pseudocode
const cities = [
'Adana', 'Adıyaman', 'Afyon', 'Ağrı', 'Amasya', 'Ankara', 'Antalya', 'Artvin',
'Aydın', 'Balıkesir', 'Bilecik', 'Bingöl', 'Bitlis', 'Bolu', 'Burdur', 'Bursa', 'Çanakkale',
'Çankırı', 'Çorum', 'Denizli', 'Diyarbakır', 'Edirne', 'Elazığ', 'Erzincan', 'Erzurum', 'Eskişehir',
'Gaziantep', 'Giresun', 'Gümüşhane', 'Hakkari', 'Hatay', 'Isparta', 'Mersin', 'İstanbul', 'İzmir',
'Kars', 'Kastamonu', 'Kayseri', 'Kırklareli', 'Kırşehir', 'Kocaeli', 'Konya', 'Kütahya', 'Malatya',
'Manisa', 'Kahramanmaraş', 'Mardin', 'Muğla', 'Muş', 'Nevşehir', 'Niğde', 'Ordu', 'Rize', 'Sakarya',
'Samsun', 'Siirt', 'Sinop', 'Sivas', 'Tekirdağ', 'Tokat', 'Trabzon', 'Tunceli', 'Şanlıurfa', 'Uşak',
'Van', 'Yozgat', 'Zonguldak', 'Aksaray', 'Bayburt', 'Karaman', 'Kırıkkale', 'Batman', 'Şırnak',
'Bartın', 'Ardahan', 'Iğdır', 'Yalova', 'Karabük', 'Kilis', 'Osmaniye', 'Düzce'
@mustafadalga
mustafadalga / validateFullName.js
Created March 25, 2021 17:29
Validate Full Name (Turkish characters are valid)
function validateFullName(name="") {
const regex= /^[A-Za-zşŞıİçÇöÖüÜĞğ ]+$/;
const fullName=String(name).trim()
let warning={
status:true,
message: ""
};
if (fullName.length==0) {
warning={
status:false,
@mustafadalga
mustafadalga / multiplePointers-countUniqueValues.js
Last active September 11, 2021 13:54
Multiple Pointers Count Unique Values Challenge:Implement a function called countUniqueValues, which accepts a sorted array, and counts the unique values in the array. There can be negative numbers in the array, but it will always be sorted.
function countUniqueValues(array){
if(array.length==0)return 0;
let i=0;
for (let j = 1; j < array.length; j++) {
if(array[i]!=array[j]){
i++;
array[i]=array[j];
}
}
return i+1;
@mustafadalga
mustafadalga / Divide-And-Conquer-Pattern.js
Created September 11, 2021 16:48
Divide And Conquer Pattern:Given a sorted array of integers, write a function called search, that accepts a value and returns the index where the value passed to the function is located. If the value is not found, return -1
function search(array, val) {
let min = 0;
let max = array.length - 1;
while (min <= max) {
let middle = Math.floor((min + max) / 2);
let currentElement = array[middle];
if (array[middle] < val) {
@mustafadalga
mustafadalga / string-search.js
Created September 19, 2021 08:49
Naive String Search Algorithm:Count the number of times a smaller string appears in a longer string
function search(text,searchText){
text=text.toLowerCase();
searchText=searchText.toLowerCase();
let count=0;
for (let i = 0; i < text.length; i++) {
if(text.substr(i,searchText.length)==searchText){
count++;
}
}
return count;
@mustafadalga
mustafadalga / bubble-sort.js
Last active October 9, 2021 18:09
Bubble sort algorithm
function bubbleSort(array) {
let noSwaps;
for (let i = array.length; i > 0; i--) {
noSwaps = true;
for (let j = 0; j < i - 1; j++) {
if (array[j] > array[j + 1]) {
@mustafadalga
mustafadalga / selection-sort.js
Created October 23, 2021 19:19
Selection Sort Algorithm
function selectionSort(array) {
for (let i = 0; i < array.length; i++) {
let lowestIndex = i
for (let j = i + 1; j < array.length; j++) {
if (array[lowestIndex] > array[j]) {
lowestIndex = j;
}