DAY 5
0
AI & Data

找LeetCode上簡單的題目來撐過30天啦(DAY5)

SQL Schema
Write a SQL query to find all duplicate emails in a table named Person.

For example, your query should return the following for the above table:

Note: All emails are in lowercase.

``````select Email from (select Email,count(*) c from Person group by Email ) as table1 where table1.c>1
``````

Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2

Constraints:
• n == nums.length
• 1 <= n <= 5 * 104
• -231 <= nums[i] <= 231 - 1

``````import java.util.HashMap;

class Solution {
public int majorityElement(int[] nums) {
HashMap<Integer,Integer> temp = new HashMap<Integer, Integer>();
int l = nums.length,i,j,max=0,vmax=0;
//System.out.print(l);
for(i=0;i<l;i++){

if(temp.containsKey(nums[i])){
temp.replace(nums[i], temp.get(nums[i])+1) ;

}else{
temp.put(nums[i],1);
}
}
Set s =temp.keySet();
//System.out.println(temp);

for(Object key : s){
if(temp.get(key)>vmax){
max = (int)key;
vmax = temp.get(key);
//System.out.println(key + ","+temp.get(key));
}
}

return max;
}
}
``````

DAY5心得