Posts

Showing posts with the label HashMap

Find Unique Element Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/find-unique-element2632/1  Solution :  HashMap<Integer,Integer>map=new HashMap<Integer,Integer>();         for(int i=0;i<n;i++){             if(map.containsKey(a[i])){                 map.put(a[i],map.get(a[i])+1);             }             else{                 map.put(a[i],1);             }         }         int c=0;         for(int a1:map.keySet()){             int b=map.get(a1);             if(b%k!=0){                 c=a1;                 break;             }...

Minimum Distances HackerRank

  class  Result {      /*      * Complete the 'minimumDistances' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY a as parameter.      */      public   static   int  minimumDistances(List<Integer> a) {         HashMap<Integer,Integer> map= new  HashMap<Integer,Integer>();         ArrayList<Integer>aa= new  ArrayList<Integer>();          for ( int  i= 0 ;i<a.size();i++){              if (!ma...

Java Collection | Set 3 (HashMap) Part-1 Geeks For Geeks

 class Solution{     static int map(int n, String keys[], int arr[], String s)     {         HashMap<String,Integer> map1=new HashMap<String,Integer>();                  for(int i=0;i<keys.length;i++){             map1.put(keys[i],arr[i]);         }         int val=0;         if(map1.containsKey(s)){            val=map1.get(s);         }         else{             val=-1;         }         return val;                } } Time: O(Length of string array or key array) [GFG Time: 0.2/2.2] Space: O(n) Thanks for Reading.😇

Sum Of Unique Elements LeetCode

Approach Explained:  Here first we will add all the numbers in the ArrayList 1 . After that we will traverse the ArrayList1 and then add all the duplicates in the ArrayList 2 . We are doing sum simultaneously for both ArrayList. Then, we will have sum of the original ArrayList (the input array) and duplicate elements sum2. We will then subtract sum2 from sum1 which is ultimately the sum of all unique elements.  First Way: TIME O(N) [LeetCode Time 3 ms] SPACE O(N) [Using of Two ArrayLists][LeetCode Memory Uasge : 36.8MB less than 34.90%]  class Solution {     public int sumOfUnique(int[] nums) {         int sum=0; int sum2=0;         ArrayList<Integer> aa=new ArrayList<Integer>(); ArrayList<Integer> ab=new ArrayList<Integer>(); for(int i=0;i<nums.length;i++){ aa.add(nums[i]); sum=sum+aa.get(i); } for(int i=0;i<aa.size();i++){ if(Collections.frequency(aa,aa.get(i))>...

Java Map Hackkerank

  //Complete this code or write your own from scratch import  java.util.*; import  java.io.*; import  java.util.HashMap; import  java.util.Map.Entry; class  Solution{      public   static   void  main(String arg[])  throws  Exception{                       InputStreamReader r= new  InputStreamReader(System.in);             BufferedReader br= new  BufferedReader(r);            int  n=Integer.valueOf(br.readLine());          if ( 1 <=n && n<= 100000 ){             HashMap<String,String> aa= new  HashMap...