First Come First Serve | Practice | GeeksforGeeks

CodeMart is a shopping platform and it is distributing gift vouchers to its esteemed users. The voucher can be redeemed by providing a fixed amount of shopping credits to the platform. One credit is sent by a user to the platform by doing one occurance in CodeMart. Since there is a huge rush of people you are required to manage the users on the basis of first come first serve. The user which came first and has exactly k occurrences at last is given the voucher first. You are given an array arr[ ] with the id's of N users in chronological order of their occurances . You are required to print the id of the user which will be given the voucher first. If no such user meets the condition print "-1".
Example 1:
Input:
N = 7 
K = 2
arr[] = {1, 7, 4, 3, 4, 8, 7} 
Output: 7
Explanation: Both 7 and 4 occur 2 times.
But 7 is the first that occurs 2 times.  
Example 2:
Input:
N = 6 
K = 1 
arr[] = {4, 1, 6, 1, 6, 4} 
Output: -1 
Explanation: No one occurs exactly 1 time.

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function firstElement() that takes array arr, integer n and integer k as input parameters and returns the required answer.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(max(arr[i])).
Constraints:
1 ≤ N ≤ 104
1 ≤ arr[i] ≤ 103
0
abhigyanpatek2 weeks ago
Simple C++ Solution:
int firstElement(int arr[], int n, int k)   {       unordered_map<int, int> map;       for(int i = 0; i<n; i++) map[arr[i]]++;       for(int i = 0; i<n; i++) if(map[arr[i]] == k) return arr[i];       return -1;   }
0
saivivekkunta22 months ago
JAVA 
class Solution { 
 
// function to find the first element 
// occurring k number of times 
   static int firstElement(int arr[], int n, int k) { 
HashMap<Integer,Integer> map = new HashMap<>();
int ans = -1;
for(int x:arr){
   if(map.containsKey(x)==false)
    {    map.put(x,1);}
  else{
      map.put(x,map.get(x)+1);
  }
}
for(int x:arr){
   if(map.get(x)==k){
       return x;
   }
}
return ans;
   }
}
 
+1
ankitparashxr2 months ago
java
 ArrayList<Integer> a = new ArrayList<>();         HashMap<Integer,Integer> hm = new HashMap<>();     for(int i:arr)     {         if(hm.containsKey(i))         {             hm.put(i,hm.get(i)+1);         }         else         {             hm.put(i,1);         }     }     for(int i:hm.keySet())     {         if(hm.get(i)==k)         {             a.add(i);         }     }     for(int i =0;i<n;i++)     {         if(a.contains(arr[i]))         {             return arr[i];         }     }     return -1;
+1
avinav26113 months ago
Easy C++ Solution
 
+1
rohanpandey7494 months ago
Easy C++ solution:
class Solution{
    public:
    int firstElement(int arr[], int n, int k)
    {
        map<int,int>mp;
        for(int i=0;i<n;i++){
            mp[arr[i]]++;
        }
        for(int i=0;i<n;i++){
            if(mp.find(arr[i])!=mp.end()){
                if(mp[arr[i]]==k){
                    return arr[i];
                }
            }
        }
        return -1;
    }
};
+1
mishraadi7336 months ago
 
 
  HashMap <Integer, Integer> mp = new HashMap<>();       for(int i = 0; i<n; i++){           mp.put(arr[i],mp.getOrDefault(arr[i],0)+1);       }        for(int i = 0; i<n; i++){           if(mp.get(arr[i]) == k){               return arr[i];           }        }        return -1; 
0
sahil01sn6 months ago
Using HashMap 
  static int firstElement(int num[], int n, int k) { 
        HashMap<Integer , Integer> map = new HashMap<Integer , Integer>();
        
        for(int i = 0 ; i < n ; i++){
            int a = 0;
            if(map.get(num[i]) != null){
                a = map.get(num[i]);
            }
            map.put(num[i] , a + 1);
        }
        
        for(int i = 0 ; i < n ; i++){
            if(map.get(num[i]) == k){
                return num[i];
            }
        }
        return -1;
        
        
        
    }
}
 
0
sudhanshu gupta1 year ago
sudhanshu gupta
int firstElement(int arr[], int n, int k)    {        unordered_map<int ,="" int=""> occ;        unordered_set<int> s;        for (int i = 0; i < n; i++)             occ[arr[i]]++;        for (auto i: occ){            if(k == (i.second)){                s.insert(i.first);            }        }        if(s.size()>0){            for(int i=0;i<n;i++){ if(s.find(arr[i])!="s.end()){" return="" arr[i];="" break;="" }="" }="" }="" else="" return="" -1;="" }="" <="" b="">
0
Mangalam1 year ago
Mangalam
LinkedHashMap<integer,integer> lhm= new  LinkedHashMap<integer,integer>();        for(int i:arr){            if(lhm.containsKey(i)){                lhm.put(i,lhm.get(i)+1);            }            else            lhm.put(i,1);        }
        for(Map.Entry<integer, integer=""> value:lhm.entrySet()){            if(value.getValue()==k){                return value.getKey();            }        }     return -1;
0
Hardik Gupta1 year ago
Hardik Gupta
My Approach
class GFG {      static int firstElement(int arr[], int n, int k) {     int max = Integer.MIN_VALUE;     for(int i: arr){        max = Math.max(max , i);    }     int [] hash = new int[max + 1];     for(int i: arr){        hash[i]++;    }     for(int i = 0; i < arr.length; i++){        int x = arr[i];        if(hash[x] == k){            return x;        }    }   return -1;    }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.