File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ var containsDuplicate = function ( nums ) {
2+ // Create a set from the nums array. Since Sets only allow unique values, any duplicates will be removed.
3+ const set = new Set ( nums ) ;
4+ // Compare the size of the set and the length of the original array.- if the size of the set is smaller than the length of the original array('nums'), it means there were duplicates.
5+
6+ return set . size < nums . length ;
7+ } ;
8+
9+ // Time Complexity: O(n); - adding elements to the Set & compare sizes
10+ // Space Complexity: O(n)
Original file line number Diff line number Diff line change 1+ var longestConsecutive = function ( nums ) {
2+ // remove the duplicates from the array and sort it in an ascending order.
3+ const setArray = [ ...new Set ( nums ) ] ;
4+ const sortedArray = setArray . sort ( ( a , b ) => a - b ) ;
5+ // create a set to store streak lengths, even when count resets.
6+ const countSet = new Set ( ) ;
7+ let count = 0 ;
8+ for ( let i = 0 ; i < sortedArray . length ; i ++ ) {
9+ if ( sortedArray [ i ] + 1 == sortedArray [ i + 1 ] ) {
10+ count += 1 ;
11+ countSet . add ( count ) ;
12+ } else {
13+ count = 0 ;
14+ }
15+ }
16+
17+ return nums . length === 0 ? 0 : countSet . size + 1 ;
18+ } ;
19+
20+ // Time complexity: O(nlogn) => TODO: need to improve this to O(n)
21+ // Space complexity: O(n)
Original file line number Diff line number Diff line change 1+ var topKFrequent = function ( nums , k ) {
2+ // 1. count the frequency of each number in the array
3+ const map = new Map ( ) ;
4+
5+ // iterating through the array to count how many times each num appears.
6+ for ( const num of nums ) {
7+ // if the num already exists in the map, increment its count
8+ if ( map . has ( num ) ) {
9+ map . set ( num , map . get ( num ) + 1 ) ;
10+ } // otherwise, set it to 1
11+ else map . set ( num , 1 ) ;
12+ }
13+
14+ // 2.create an array to store the freqeuncy numbers
15+ const freqArr = [ ] ;
16+ for ( const [ num , freq ] of map ) {
17+ freqArr . push ( [ num , freq ] ) ;
18+ }
19+ // sort in descending order by frequency
20+ freqArr . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
21+ return freqArr . slice ( 0 , k ) . map ( ( [ num ] ) => num ) ;
22+ } ;
23+
24+ // Time complexity: O(nlogn)
25+ // Space complexity: O(n)
Original file line number Diff line number Diff line change 1+ var isPalindrome = function ( s ) {
2+ // remove any special characters and space from the string
3+ const formattedString = s . toLowerCase ( ) . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ;
4+ // use split() method to separate each charaters and put them in an array - reverse it - concatenate
5+ const reversedString = formattedString . split ( '' ) . reverse ( ) . join ( '' ) ;
6+
7+ return reversedString === formattedString ;
8+ } ;
9+
10+ // time complexity: O(n)
11+ // space complexity: O(n)
You can’t perform that action at this time.
0 commit comments