File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change 22 * @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
33 */
44
5- // Complete the freqQuery function below.
65export function freqQuery ( queries ) {
76 const result = [ ] ;
87 const dataMap = { } ;
@@ -12,6 +11,9 @@ export function freqQuery(queries) {
1211 const __DELETE__ = 2 ;
1312 const __SELECT__ = 3 ;
1413
14+ const __NOT_FOUND__ = 0 ;
15+ const __FOUND__ = 1 ;
16+
1517 queries . forEach ( ( query ) => {
1618 const [ operation , data ] = query ;
1719
@@ -24,18 +26,23 @@ export function freqQuery(queries) {
2426 case __DELETE__ :
2527 dataMap [ data ] = Math . max ( 0 , current - 1 ) ;
2628 break ;
27- case __SELECT__ :
28- for ( const [ key , value ] of Object . entries ( dataMap ) ) {
29+ case __SELECT__ : {
30+ let i = 0 ;
31+ const entries = Object . entries ( dataMap ) ;
32+
33+ for ( const [ key , value ] of entries ) {
2934 console . log ( key , value ) ;
3035 if ( value === data ) {
31- result . push ( 1 ) ;
36+ result . push ( __FOUND__ ) ;
3237 break ;
3338 }
39+ i += 1 ;
3440 }
35- if ( result . length === 0 ) {
36- result . push ( 0 ) ;
41+ if ( i === entries . length ) {
42+ result . push ( __NOT_FOUND__ ) ;
3743 }
3844 break ;
45+ }
3946 default :
4047 throw new Error ( 'Invalid operation' ) ;
4148 }
You can’t perform that action at this time.
0 commit comments