@@ -9743,6 +9743,354 @@ console.log("The number of words in the string is: " + words.length);
9743
9743
// Output: The number of words in the string is: 2
9744
9744
` ` `
9745
9745
9746
+ Find the largest element in an array.
9747
+
9748
+ ` ` ` javascript
9749
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9750
+
9751
+ const largest = Math .max (... numbers);
9752
+
9753
+ console .log (" Largest number: " + largest);
9754
+
9755
+ // Output: Largest number: 30
9756
+ ` ` `
9757
+
9758
+ Find the smallest element in an array.
9759
+
9760
+ ` ` ` javascript
9761
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9762
+
9763
+ const smallest = Math .min (... numbers);
9764
+
9765
+ console .log (" Smallest number: " + smallest);
9766
+
9767
+ // Output: Smallest number: 5
9768
+ ` ` `
9769
+
9770
+ Find the largest element in an array without using any built-in functions/methods.
9771
+
9772
+ ` ` ` javascript
9773
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9774
+
9775
+ let largest = numbers[0 ];
9776
+
9777
+ for (let i = 1 ; i < numbers .length ; i++ ) {
9778
+ if (numbers[i] > largest) {
9779
+ largest = numbers[i];
9780
+ }
9781
+ }
9782
+
9783
+ console .log (" Largest number: " + largest);
9784
+
9785
+ // Output: Largest number: 30
9786
+ ` ` `
9787
+
9788
+ Find the smallest element in an array without using any built-in functions/methods.
9789
+
9790
+ ` ` ` javascript
9791
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9792
+
9793
+ let smallest = numbers[0 ];
9794
+
9795
+ for (let i = 1 ; i < numbers .length ; i++ ) {
9796
+ if (numbers[i] < smallest) {
9797
+ smallest = numbers[i];
9798
+ }
9799
+ }
9800
+
9801
+ console .log (" Smallest number: " + smallest);
9802
+
9803
+ // Output: Smallest number: 5
9804
+ ` ` `
9805
+
9806
+ Reverse an array without using any built-in functions/methods.
9807
+
9808
+ ` ` ` javascript
9809
+ const numbers = [1 , 2 , 3 , 4 , 5 ];
9810
+
9811
+ for (let i = 0 ; i < (numbers .length - 1 ) / 2 ; i++ ) {
9812
+ const temp = numbers[i];
9813
+ numbers[i] = numbers[numbers .length - 1 - i];
9814
+ numbers[numbers .length - 1 - i] = temp;
9815
+ }
9816
+
9817
+ console .log (" Reversed array: " + numbers);
9818
+
9819
+ // Output: Reversed array: 5,4,3,2,1
9820
+ ` ` `
9821
+
9822
+ Find the missing number in an array of size n containing numbers from 1 to n+1 without using any built-in functions/methods.
9823
+
9824
+ ` ` ` javascript
9825
+ const numbers = [1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 , 10 ];
9826
+
9827
+ const n = numbers .length + 1 ;
9828
+
9829
+ let sum = (n * (n + 1 )) / 2 ;
9830
+
9831
+ for (let i = 0 ; i < numbers .length ; i++ ) {
9832
+ sum -= numbers[i];
9833
+ }
9834
+
9835
+ console .log (" The missing number is: " + sum);
9836
+
9837
+ // Output: The missing number is: 5
9838
+ ` ` `
9839
+
9840
+ Move all zeroes to the end of the array without using any built-in functions/methods.
9841
+
9842
+ ` ` ` javascript
9843
+ const numbers = [0 , 2 , 0 , 4 , 0 , 6 , 0 , 8 ];
9844
+
9845
+ let count = 0 ;
9846
+
9847
+ for (let i = 0 ; i < numbers .length ; i++ ) {
9848
+ if (numbers[i] !== 0 ) {
9849
+ numbers[count] = numbers[i];
9850
+ count++ ;
9851
+ }
9852
+ }
9853
+
9854
+ while (count < numbers .length ) {
9855
+ numbers[count] = 0 ;
9856
+ count++ ;
9857
+ }
9858
+
9859
+ console .log (" Array with zeroes at the end: " + numbers);
9860
+
9861
+ // Output: Array with zeroes at the end: 2,4,6,8,0,0,0,0
9862
+ ` ` `
9863
+
9864
+ Find the "Kth" largest element in an array without using any built-in functions/methods.
9865
+
9866
+ ` ` ` javascript
9867
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9868
+
9869
+ const k = 2 ;
9870
+
9871
+ for (let i = 0 ; i < k; i++ ) {
9872
+ let max = 0 ;
9873
+ let maxIndex = 0 ;
9874
+
9875
+ for (let j = 0 ; j < numbers .length ; j++ ) {
9876
+ if (numbers[j] > max) {
9877
+ max = numbers[j];
9878
+ maxIndex = j;
9879
+ }
9880
+ }
9881
+
9882
+ numbers[maxIndex] = 0 ;
9883
+ }
9884
+
9885
+ console .log (" The " + k + " th largest element is: " + max);
9886
+
9887
+ // Output: The 2th largest element is: 20
9888
+ ` ` `
9889
+
9890
+ Find the "Kth" smallest element in an array without using any built-in functions/methods.
9891
+
9892
+ ` ` ` javascript
9893
+ const numbers = [10 , 5 , 20 , 15 , 30 ];
9894
+
9895
+ const k = 2 ;
9896
+
9897
+ for (let i = 0 ; i < k; i++ ) {
9898
+ let min = numbers[i];
9899
+ let minIndex = i;
9900
+
9901
+ for (let j = i + 1 ; j < numbers .length ; j++ ) {
9902
+ if (numbers[j] < min) {
9903
+ min = numbers[j];
9904
+ minIndex = j;
9905
+ }
9906
+ }
9907
+
9908
+ // Swap the found minimum element with the element at index i
9909
+ [numbers[i], numbers[minIndex]] = [numbers[minIndex], numbers[i]];
9910
+ }
9911
+
9912
+ console .log (" The " + k + " th smallest element is: " + numbers[k - 1 ]);
9913
+
9914
+ // Output: The 2th smallest element is: 10
9915
+ ` ` `
9916
+
9917
+ Find the subarray with the given sum in a non-negative array without using any built-in functions/methods.
9918
+
9919
+ ` ` ` javascript
9920
+ const numbers = [1 , 4 , 20 , 3 , 10 , 5 ];
9921
+
9922
+ const targetSum = 33 ;
9923
+
9924
+ let start = 0 ;
9925
+
9926
+ let sum = numbers[0 ];
9927
+
9928
+ for (let i = 1 ; i <= numbers .length ; i++ ) {
9929
+ while (sum > targetSum && start < i - 1 ) {
9930
+ sum -= numbers[start];
9931
+ start++ ;
9932
+ }
9933
+
9934
+ if (sum === targetSum) {
9935
+ console .log (" Subarray with the given sum found between indexes " + start + " and " + (i - 1 ));
9936
+ break ;
9937
+ }
9938
+
9939
+ if (i < numbers .length ) {
9940
+ sum += numbers[i];
9941
+ }
9942
+ }
9943
+
9944
+ // Output: Subarray with the given sum found between indexes 2 and 4
9945
+ ` ` `
9946
+
9947
+ Kadane’s Algorithm: Find the maximum sum subarray without using any built-in functions/methods.
9948
+
9949
+ ` ` ` javascript
9950
+ const numbers = [- 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ];
9951
+
9952
+ let maxSum = numbers[0 ];
9953
+
9954
+ let currentSum = numbers[0 ];
9955
+
9956
+ for (let i = 1 ; i < numbers .length ; i++ ) {
9957
+ if (currentSum + numbers[i] > numbers[i]) {
9958
+ currentSum = currentSum + numbers[i];
9959
+ } else {
9960
+ currentSum = numbers[i];
9961
+ }
9962
+
9963
+ if (currentSum > maxSum) {
9964
+ maxSum = currentSum;
9965
+ }
9966
+ }
9967
+
9968
+ console .log (" Maximum sum subarray: " + maxSum);
9969
+
9970
+ // Output: Maximum sum subarray: 7
9971
+ ` ` `
9972
+
9973
+ Two Sum Problem: Find pairs in an array that sum to a target value without using any built-in functions/methods.
9974
+
9975
+ ` ` ` javascript
9976
+ const numbers = [2 , 7 , 11 , 15 ];
9977
+
9978
+ const target = 9 ;
9979
+
9980
+ const pairs = [];
9981
+
9982
+ for (let i = 0 ; i < numbers .length ; i++ ) {
9983
+ for (let j = i + 1 ; j < numbers .length ; j++ ) {
9984
+ if (numbers[i] + numbers[j] === target) {
9985
+ pairs[pairs .length ] = [numbers[i], numbers[j]];
9986
+ }
9987
+ }
9988
+ }
9989
+
9990
+ console .log (" Pairs that sum to " + target + " : " + pairs);
9991
+
9992
+ // Output: Pairs that sum to 9: 2,7
9993
+ ` ` `
9994
+
9995
+ Merge two sorted arrays without using any built-in functions/methods.
9996
+
9997
+ ` ` ` javascript
9998
+ const arr1 = [1 , 3 , 5 , 7 ];
9999
+ const arr2 = [2 , 4 , 6 , 8 ];
10000
+
10001
+ const mergedArray = [];
10002
+
10003
+ let i = 0 ;
10004
+
10005
+ let j = 0 ;
10006
+
10007
+ while (i < arr1 .length && j < arr2 .length ) {
10008
+ if (arr1[i] < arr2[j]) {
10009
+ mergedArray[mergedArray .length ] = arr1[i];
10010
+ i++ ;
10011
+ } else {
10012
+ mergedArray[mergedArray .length ] = arr2[j];
10013
+ j++ ;
10014
+ }
10015
+ }
10016
+
10017
+ while (i < arr1 .length ) {
10018
+ mergedArray[mergedArray .length ] = arr1[i];
10019
+ i++ ;
10020
+ }
10021
+
10022
+ while (j < arr2 .length ) {
10023
+ mergedArray[mergedArray .length ] = arr2[j];
10024
+ j++ ;
10025
+ }
10026
+
10027
+ console .log (" Merged array: " + mergedArray);
10028
+
10029
+ // Output: Merged array: 1,2,3,4,5,6,7,8
10030
+ ` ` `
10031
+
10032
+ Find the longest increasing subsequence in an array without using any built-in functions/methods.
10033
+
10034
+ ` ` ` javascript
10035
+ const numbers = [10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 , 80 ];
10036
+
10037
+ const lis = [];
10038
+ for (let i = 0 ; i < numbers .length ; i++ ) {
10039
+ lis[i] = 1 ;
10040
+ }
10041
+
10042
+ for (let i = 1 ; i < numbers .length ; i++ ) {
10043
+ for (let j = 0 ; j < i; j++ ) {
10044
+ if (numbers[i] > numbers[j] && lis[i] < lis[j] + 1 ) {
10045
+ lis[i] = lis[j] + 1 ;
10046
+ }
10047
+ }
10048
+ }
10049
+
10050
+ let maxLis = 0 ;
10051
+
10052
+ for (let i = 0 ; i < lis .length ; i++ ) {
10053
+ if (lis[i] > maxLis) {
10054
+ maxLis = lis[i];
10055
+ }
10056
+ }
10057
+
10058
+ console .log (" Length of the longest increasing subsequence: " + maxLis);
10059
+
10060
+ // Output: Length of the longest increasing subsequence: 6
10061
+ ` ` `
10062
+
10063
+ Trapping Rain Water Problem without using any built-in functions/methods.
10064
+
10065
+ ` ` ` javascript
10066
+ const heights = [0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 ];
10067
+
10068
+ let totalWater = 0 ;
10069
+
10070
+ for (let i = 1 ; i < heights .length - 1 ; i++ ) {
10071
+ let leftMax = 0 ;
10072
+ for (let j = 0 ; j < i; j++ ) {
10073
+ if (heights[j] > leftMax) {
10074
+ leftMax = heights[j];
10075
+ }
10076
+ }
10077
+
10078
+ let rightMax = 0 ;
10079
+ for (let j = i + 1 ; j < heights .length ; j++ ) {
10080
+ if (heights[j] > rightMax) {
10081
+ rightMax = heights[j];
10082
+ }
10083
+ }
10084
+
10085
+ const minMax = leftMax < rightMax ? leftMax : rightMax;
10086
+ totalWater += minMax > heights[i] ? minMax - heights[i] : 0 ;
10087
+
10088
+ console .log (" Total trapped rainwater: " + totalWater);
10089
+
10090
+ // Output: Total trapped rainwater: 6
10091
+ }
10092
+ ` ` `
10093
+
9746
10094
[Back to Top⤴️](#table-of-contents)
9747
10095
9748
10096
## List of GitHub Repositories to learn JavaScript
0 commit comments