User:Coding booth/sandbox

From Wikipedia, the free encyclopedia
  • Comment: No idea what this is, but it isn't a viable encyclopaedia article draft. DoubleGrazing (talk) 12:08, 22 February 2024 (UTC)


HashMap problems :

// You are given an array of integers that contain numbers in random order. Write a program to find and return the number which occurs the maximum times in the given input. If two or more elements are having the maximum frequency, return the element which occurs in the array first.

import java.util.HashMap; public class Solution {

   public static int maxFrequencyNumber(int[] arr){ 

HashMap <Integer, Integer> map=new HashMap<>(); for (int i = 0; i < arr.length; i++) {

           if (map.containsKey(arr[i])) {
               map.put(arr[i], map.get(arr[i]) + 1);
           } else {
               map.put(arr[i], 1);
           }
       }
       int maxFreqElement = arr[0];
       int maxFreq = map.get(arr[0]);
       for (int i = 1; i < arr.length; i++) {
           if (map.get(arr[i]) > maxFreq) {
               maxFreqElement = arr[i];
               maxFreq = map.get(arr[i]);
           }
       }
       return maxFreqElement;
   }

}

// Problem statement You have been given two integer arrays/lists (ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.

import java.util.HashMap; public class Solution {

public static void printIntersection(int[] arr1,int[] arr2){ HashMap<Integer, Integer> map = new HashMap<>();

       for (int num : arr1) {
           map.put(num, map.getOrDefault(num, 0) + 1);
       }
       for (int num : arr2) {
           if (map.containsKey(num) && map.get(num) > 0) {
               System.out.println(num);
               map.put(num, map.get(num) - 1); 
           }
       }

} }

// Problem statement Given a random integer array A of size N. Find and print the count of pair of elements in the array which sum up to 0.

Note: Array A can contain duplicate elements as well.

import java.util.*;

public class Solution {

   public static int PairSum(int[] input, int size) {
       HashMap<Integer, Integer> map = new HashMap<>();
       int count = 0;
       
       for (int num : input) {
           map.put(num, map.getOrDefault(num, 0) + 1);
       }
       
       for (int num : input) {
           int complement = -num;
           if (map.containsKey(complement)) {
               count += map.get(complement);
           }
           if (complement == num) {
               count--;
           }
       }
       
       return count / 2;
   }

}

//Problem statement Given a string S, you need to remove all the duplicates. That means, the output string should contain each character only once. The respective order of characters should remain same, as in the input string.

import java.util.HashMap;

public class Solution {

   public static String uniqueChar(String str) {
       StringBuilder result = new StringBuilder();
       HashMap<Character, Integer> charMap = new HashMap<>();
       for (int i = 0; i < str.length(); i++) {
           char ch = str.charAt(i);
           if (!charMap.containsKey(ch)) {
               result.append(ch);
               charMap.put(ch, 1);
           }
       }
       return result.toString();
   }

//Problem statement Given an array consisting of positive and negative integers, find the length of the longest subarray whose sum is zero.

import java.util.HashMap;

public class Solution {

   public static int lengthOfLongestSubsetWithZeroSum(int arr[]) {
       HashMap<Integer, Integer> sumMap = new HashMap<>();
       int maxLen = 0;
       int sum = 0;
       for (int i = 0; i < arr.length; i++) {
           sum += arr[i];
           if (sum == 0) {
               maxLen = i + 1;
           } else {
               if (sumMap.containsKey(sum)) {
                   maxLen = Math.max(maxLen, i - sumMap.get(sum));
               } else {
                   sumMap.put(sum, i);
               }
           }
       }
       return maxLen;
   }

} //Problem statement You are given an array of unique integers that contain numbers in random order. You have to find the longest possible sequence of consecutive numbers using the numbers from given array.

You need to return the output array which contains starting and ending element. If the length of the longest possible sequence is one, then the output array must contain only single element. import java.util.ArrayList; import java.util.HashMap;

public class Solution {

   public static ArrayList<Integer> longestConsecutiveIncreasingSequence(int[] arr) {
       HashMap<Integer, Boolean> numMap = new HashMap<>();
       for (int num : arr) {
           numMap.put(num, true);
       }
       int maxLength = 0;
       int startNum = 0;
       for (int num : arr) {
           if (!numMap.containsKey(num - 1)) {                int length = 1;
               int currNum = num;
               while (numMap.containsKey(currNum + 1)) {
                   length++;
                   currNum++;
               }
                   if (length > maxLength) {
                   maxLength = length;
                   startNum = num;
               }
           }
       }
       ArrayList<Integer> result = new ArrayList<>();
       result.add(startNum); 
       result.add(startNum + maxLength - 1);
       return result;
   }

} //Problem statement You are given with an array of integers and an integer K. You have to find and print the count of all such pairs which have difference K.

Note: Take absolute difference between the elements of the array.

import java.util.HashMap;

public class Solution {

   public static int getPairsWithDifferenceK(int arr[], int k) {
       HashMap<Integer, Integer> frequencyMap = new HashMap<>();
       for (int num : arr) {
           frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
       }
       int count = 0;
       if (k == 0) {
           for (int frequency : frequencyMap.values()) {
               count += (frequency * (frequency - 1)) / 2;
           }
       } else {
           for (int num : frequencyMap.keySet()) {
               if (frequencyMap.containsKey(num + k)) {
                   count += frequencyMap.get(num) * frequencyMap.get(num + k);
               }
           }
       }
       return count;
   }

}

}Bold text