From 0017c8c354f9177a17567b97c88fa0648795cf6d Mon Sep 17 00:00:00 2001 From: Arpita Jadhav <89975545+arpitajadhav155@users.noreply.github.com> Date: Sun, 15 Oct 2023 11:37:28 +0530 Subject: [PATCH] Add files via upload --- BinarySearch.java | 26 ++++++++++++++++++++++++ HashMapInJava.java | 47 +++++++++++++++++++++++++++++++++++++++++++ RemoveDuplicates.java | 21 +++++++++++++++++++ SubArrayZeroSum.java | 10 +++++++++ TwoSum.java | 28 ++++++++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 BinarySearch.java create mode 100644 HashMapInJava.java create mode 100644 RemoveDuplicates.java create mode 100644 SubArrayZeroSum.java create mode 100644 TwoSum.java diff --git a/BinarySearch.java b/BinarySearch.java new file mode 100644 index 0000000..d0e4fb5 --- /dev/null +++ b/BinarySearch.java @@ -0,0 +1,26 @@ +public class BinarySearch { + public static void search(int arr[],int key,int s,int e){ + // int s=0; + // int e=arr.length-1; + int m=(s+e)/2; + while(s<=e){ + if(arr[m]==key){ + System.out.println("Element found at index = "+ m); + break; + } + + else if(arr[m] map = new HashMap<>(); + + //Insertion + map.put("India", 120); + map.put("US", 30); + map.put("China", 150); + + System.out.println(map); + + + map.put("China", 180); + System.out.println(map); + + //Searching + if(map.containsKey("Indonesia")) { + System.out.println("key is present in the map"); + } else { + System.out.println("key is not present in the map"); + } + + System.out.println(map.get("China")); //key exists + System.out.println(map.get("Indonesia")); //key doesn't exist + + //Iteration (1) + for( Map.Entry e : map.entrySet()) { + System.out.println(e.getKey()); + System.out.println(e.getValue()); + } + + //Iteration (2) + Set keys = map.keySet(); + for(String key : keys) { + System.out.println(key+ " " + map.get(key)); + } + + //Removing + map.remove("China"); + System.out.println(map); + + + } + +} diff --git a/RemoveDuplicates.java b/RemoveDuplicates.java new file mode 100644 index 0000000..cdee14a --- /dev/null +++ b/RemoveDuplicates.java @@ -0,0 +1,21 @@ +import java.util.*; +public class RemoveDuplicates { + public static int[] removeDup(int arr[]){ + HashSet set=new HashSet<>(); + for(int num:arr){ + set.add(num); + } + int unique[]=new int[set.size()]; + int i=0; + for(int num:set){ + unique[i++]=num; + } + return unique; + } + public static void main(String[] args) { + int arr[]={4,59,6,6,1,4,59}; + int unique[]=removeDup(arr); + System.out.println(Arrays.toString(unique)); + + } +} diff --git a/SubArrayZeroSum.java b/SubArrayZeroSum.java new file mode 100644 index 0000000..c92381f --- /dev/null +++ b/SubArrayZeroSum.java @@ -0,0 +1,10 @@ +import java.util.*; +public class SubArrayZeroSum { + public static int[] find(int arr[]){ + Map sumMap=new HashMap<>(); + } + public static void main(String[] args) { + int arr[]={4,5,3,-3,2,-2}; + int final[]=find(arr); + } +} diff --git a/TwoSum.java b/TwoSum.java new file mode 100644 index 0000000..6bce702 --- /dev/null +++ b/TwoSum.java @@ -0,0 +1,28 @@ +import java.util.HashMap; +import java.util.*; + +public class TwoSum { + +public static int[] findTwoSum(int[] nums,int target){ + Map map = new HashMap<>(); + for(int i=0;i