Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. // Find the number of characters in a string
  2. function charCount(str) {
  3. let result = {};
  4. for(let char of str) {
  5. if(/[a-z0-9]/.test(char)) { // includes only lowercase a,b....z and 0,1....9
  6. result[char] ? result[char]++ : result[char] = 1;
  7. }
  8. }
  9. return result;
  10. }
  11.  
  12. // Find whether the two strings are of anagram
  13. function anagram(strOne, strTwo) {
  14. if(strOne.length != strTwo.length) {
  15. return false;
  16. }
  17.  
  18. let populatedStrOne = {};
  19. for(let char of strOne) {
  20. populatedStrOne[char] = (populatedStrOne[char] || 0) + 1;
  21. }
  22.  
  23. for(let char of strTwo) {
  24. if(!populatedStrOne[char]) {
  25. return false;
  26. } else {
  27. populatedStrOne[char] -= 1;
  28. }
  29. }
  30.  
  31. return true;
  32. }
  33.  
  34. // Find the coordinates in the given array
  35. function sumZero(coordinates) {
  36. let result = [];
  37. for(let i = 0; i < coordinates.length; i++) {
  38. for(let j = i + 1; j < coordinates.length; j++) {
  39. if(coordinates[i] + coordinates[j] == 0) {
  40. result.push(`${coordinates[i]}, ${coordinates[j]}`);
  41. }
  42. }
  43. }
  44.  
  45. return result;
  46. }
  47.  
  48. // Find the unique values in the given array
  49. function countUniqueValues(arr) {
  50. let populatedObj = {};
  51. let result = [];
  52.  
  53. for(let el of arr) {
  54. populatedObj[el] = (populatedObj[el] || 0) + 1;
  55. }
  56.  
  57. for(let key in populatedObj) {
  58. if(populatedObj[key] == 1) {
  59. result.push(key);
  60. }
  61. }
  62.  
  63. return result;
  64. }
  65.  
  66. /* Searching Algorithms
  67. * LinearSearch
  68. * BinarySearch
  69. */
  70.  
  71. // Linear Search
  72. function LinearSearch(arr, num) {
  73. for(let i = 0; i < arr.length; i++) {
  74. if(arr[i] == num) {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80.  
  81. // Binary Search
  82. function BinarySearch(arr, num) {
  83. let start = 0;
  84. let end = arr.length - 1;
  85. let middle = Math.floor((start + end) / 2);
  86.  
  87. while(num != arr[middle] && start <= end) {
  88. if(num > arr[middle]) {
  89. start = middle + 1;
  90. } else {
  91. end = middle - 1;
  92. }
  93.  
  94. middle = Math.floor((start + end) / 2);
  95. }
  96.  
  97. return arr[middle] == num ? middle : -1;
  98. }
  99.  
  100. /* Sorting
  101. * BubbleSort
  102. */
  103.  
  104. // BubbleSort
  105. function BubbleSort(arr) {
  106. for(let i = 0; i < arr.length; i++) {
  107. for(let j = i + 1; j < arr.length; j++) {
  108. if(arr[i] > arr[j]) {
  109. let temp = arr[i];
  110. arr[i] = arr[j];
  111. arr[j] = temp;
  112. }
  113. }
  114. }
  115.  
  116. return arr;
  117. }
  118.  
  119. // OptmisedBubbleSort
  120. function OptimisedBubbleSort(arr) {
  121. let noSwaps;
  122. for(let i = 0; i < arr.length; i++) {
  123. noSwaps = true
  124. for(let j = i + 1; j < arr.length; j++) {
  125. if(arr[i] > arr[j]) {
  126. let temp = arr[i];
  127. arr[i] = arr[j];
  128. arr[j] = temp;
  129. noSwaps = false;
  130. }
  131. }
  132. if(noSwaps) break;
  133. }
  134.  
  135. return arr;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement