Set Mismatch LeetCode
Problem Link:
https://leetcode.com/problems/set-mismatch/
Solution 1:
int sum=0;
int sum1=0;
HashSet<Integer>set=new HashSet<Integer>();
for(int i=0;i<nums.length;i++){
set.add(nums[i]);
sum1=sum1+nums[i];
}
for(int a:set){
sum=sum+a;
}
int arr[]=new int[2];
arr[0]=sum1-sum;
int sum2=(nums.length*(nums.length+1))/2;
arr[01]=sum2-sum;
return arr;
----------------------------------------------------------------------------------------------------------------------------
Solution 2:
Arrays.sort(nums);
Stack<Integer>stack=new Stack<Integer>();
int a=0;
int sum=0;
for(int i=0;i<nums.length;i++){
if(stack.isEmpty()){
stack.push(nums[i]);
}
else{
if(stack.peek()==nums[i]){
a=stack.peek();
// stack.pop();
}
else{
stack.push(nums[i]);
}
}
}
while(!stack.isEmpty()){
sum=sum+stack.pop();
}
int arr[]=new int[2];
arr[0]=a;
arr[1]=(nums.length*(nums.length+1))/2-sum;
return arr;
Comments
Post a Comment