Ad
Code
Diff
  • class Solution {
        public static void main(String[] args) {
            System.out.println("What is next?");
        }
    }
    • class Solution {
    • public static void main(String[] args) {
    • System.out.println("Hello darkness, my old friend...");
    • System.out.println("What is next?");
    • }
    • }
Code
Diff
  • import java.util.*;
    import java.util.List;
    import java.util.Map;
    import java.util.Optional;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    /**
      Time Complexity  : O(N)
      Space Complexity : O(N)
    */
    class MaxOccurence {
        
      public static int findMax(int[] nums) {
        Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
                .boxed()
                .collect(Collectors.groupingBy(num -> num))
                .entrySet()
                .stream()
                .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
            
        return max.isPresent() ? max.get().getKey() : -1;
      }
    }
    • import java.util.*;
    • import java.util.List;
    • import java.util.Map;
    • import java.util.Optional;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • /**
    • Time Complexity : O(N)
    • Space Complexity : O(N)
    • */
    • class MaxOccurence {
    • public static int findMax(int[] nums) {
    • return findMaxOccurenceLinkedHashMap(nums);
    • Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
    • .boxed()
    • .collect(Collectors.groupingBy(num -> num))
    • .entrySet()
    • .stream()
    • .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
    • return max.isPresent() ? max.get().getKey() : -1;
    • }
    • private static int findMaxOccurenceLinkedHashMap(int[] nums) {
    • int maxKey = 0, maxNum = 0;
    • if(nums.length < 1) return -1;
    • if(nums.length == 1) return nums[0];
    • Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>(nums.length);
    • for(int i = 0, len = nums.length; i < len; i++) {
    • if(!counts.containsKey(nums[i])) {
    • counts.put(nums[i], 1);
    • } else {
    • counts.put(nums[i], (counts.get(nums[i]) + 1));
    • }
    • }
    • for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
    • if (entry.getValue() > maxNum) {
    • maxKey = entry.getKey();
    • maxNum = entry.getValue();
    • }
    • }
    • return maxKey;
    • }
    • }
Code
Diff
  • import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Primes {
        public static boolean isPrime(int n) {
            if (n == 2)
                return true;
    
            if (n % 2 == 0) return false;
    
            for (int i = 3; i * i <= n; i += 2) {
                if (n % i == 0)
                    return false;
            }
            return true;
        }
        
        public static List<Integer> generatePrimes(int min, int max) {
            if (!(min > 0 && max > 0))
                return null;
    
            return IntStream.range(min, max)
                    .filter(e -> isPrime(e))
                    .boxed()
                    .collect(Collectors.toList());
        }
    
    }
    • import java.util.*;
    • import java.util.List;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • public class Primes {
    • public static List<Integer> generatePrimes(int min, int max) {
    • List<Integer> primes = new ArrayList<Integer>();
    • boolean isPrime = false;
    • if((min > max) || min < 0 || max <= 0) {
    • return null;
    • }
    • for(int i = min; i <= max; i++) {
    • long endLimit = (long)Math.floor(Math.sqrt(i));
    • isPrime = true;
    • for(long j = 2; j <= endLimit; j++) {
    • if (i % j == 0) {
    • isPrime = false;
    • break;
    • public static boolean isPrime(int n) {
    • if (n == 2)
    • return true;
    • if (n % 2 == 0) return false;
    • for (int i = 3; i * i <= n; i += 2) {
    • if (n % i == 0)
    • return false;
    • }
    • }
    • if(isPrime) {
    • primes.add(i);
    • }
    • return true;
    • }
    • return primes;
    • }
    • public static List<Integer> generatePrimes(int min, int max) {
    • if (!(min > 0 && max > 0))
    • return null;
    • return IntStream.range(min, max)
    • .filter(e -> isPrime(e))
    • .boxed()
    • .collect(Collectors.toList());
    • }
    • }
Hashes
Data Structures
Code
Diff
  • /* HashMap Example */
     
    import java.util.*;
    import java.lang.*;
    import java.io.*;
     
    class HashMapDemo
    {
      public static void main (String[] args) throws java.lang.Exception {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
            put(1, 1);
            put(2, 1);
            put(3, 1);
            put(4, 1);
            put(5, 1);
            put(6, 1);
        }};
    
        map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
      }
    }
    • /* HashMap Example */
    • import java.util.*;
    • import java.lang.*;
    • import java.io.*;
    • class HashMapDemo
    • {
    • public static void main (String[] args) throws java.lang.Exception
    • {
    • Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    • map.put(1, 1);
    • map.put(2, 1);
    • map.put(3, 1);
    • map.put(4, 1);
    • map.put(5, 1);
    • map.put(6, 1);
    • for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    • int key = entry.getKey();
    • int value = entry.getValue();
    • System.out.println(key + " " + value);
    • }
    • }
    • public static void main (String[] args) throws java.lang.Exception {
    • Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
    • put(1, 1);
    • put(2, 1);
    • put(3, 1);
    • put(4, 1);
    • put(5, 1);
    • put(6, 1);
    • }};
    • map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
    • }
    • }