
Concept explainers
Remove dublicates.
import java.util.ArrayList;
import java.util.HashMap;
/*
remove duplicates from the array and
return the unique values in A ArrayList,
always nmaintain the order
*/
public class RemoveDuplicates {
public static ArrayList<Integer> removeDuplicates(int arr[]) {
ArrayList<Integer> output = new ArrayList<>();
HashMap<Integer, Boolean> seen = new HashMap<>();
for (Integer element : arr) {
if (seen.containsKey(element)) {
continue;
}
output.add(element);
seen.put(element, true);
}
return output;
}
// Driver code to check our function
public static void main(String[] args) {
int arr[] = {1,3,1,4,5,100000, 200, 5,100000, 4};
ArrayList<Integer> output = removeDuplicates(arr);
for(int i=0;i<output.size();i++){
System.out.print(output.get(i)+" ");
//1 3 4 5 100000 200 is the output, the order of the array is maintained
}
}
}.

Step by stepSolved in 3 steps with 1 images

- Please answer the problem in the screenshot. Please use the methods below as a base. The language is in Java. import java.util.*; class HeapMax { // we go with arraylist instead of array for size flexibility private ArrayList<Integer> data; // DO NOT MODIFY THIS METHOD public HeapMax() { data = new ArrayList<Integer>(0); } // insert a new element and restore max heap property public void insert(int element) { } // return max public int getMax() { // remove this line return 0; } // remove max and restore max heap property public int removeMax() { // remove this line return 0; } // heap builder public void build(int[] arr) { } // print out heap as instructed in the handout public void display() { } // you are welcome to add any supporting methods }arrow_forwardimport java.util.HashSet; import java.util.Set; // Define a class named LinearSearchSet public class LinearSearchSet { // Define a method named linearSearch that takes in a Set and an integer target // as parameters public static boolean linearSearch(Set<Integer> set, int target) { // Iterate over all elements in the Set for () { // Check if the current value is equal to the target if () { // If so, return true } } // If the target was not found, return false } // Define the main method public static void main(String[] args) { // Create a HashSet of integers and populate integer values Set<Integer> numbers = new HashSet<>(); // Define the target to search for numbers.add(3); numbers.add(6); numbers.add(2); numbers.add(9); numbers.add(11); // Call the linearSearch method with the set…arrow_forwardHow do I sort tiles from least valuable to most valuable? import java.util.ArrayList;class Rack{private ArrayList<Tile> Tile;public Rack(){Tile = new ArrayList<Tile>();}public void addTile(Tile t){Tile.add(t);}public String toString(){String str = "";for(int i=0; i < Tile.size() ; i++)str = str + Tile.get(i);return str;}public void sortHighToLow(){int i, j, min;int n = Tile.size();for (i = 0; i < n-1; i++){min = i;for (j = i+1; j < n; j++){if(Tile.get(j).compareTo(Tile.get(min)) < 0)min = j;}//Swap.Tile t = Tile.get(min);Tile t1 = Tile.get(i);Tile.set(min, t1);Tile.set(i, t);}}}class Main{public static void main(String[] args){Rack craigTiles = new Rack();craigTiles.addTile(new Tile('H', 4));craigTiles.addTile(new Tile('E', 1));craigTiles.addTile(new Tile('L', 1));craigTiles.addTile(new Tile('L', 1));craigTiles.addTile(new Tile('O', 1));craigTiles.addTile(new Tile('Z', 10));craigTiles.addTile(new Tile('Y',…arrow_forward
- Given an array of numbers, and an integer, find the last index that that integer appears in the array. If the number is not found, return -1. import java.util.ArrayList;public class LastIndexFound{public static int solution(ArrayList<Integer> nums, int numToFind){// ↓↓↓↓ your code goes here ↓↓↓↓return 0;}}arrow_forwardIn java Write a method public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) that merges one array list with another. Ex: a = 1 2 3 4 5, b = 6 7 8 9 10 then merge returns 1 6 2 7 3 8 4 9 5 10arrow_forwardTrace insertion sort for list ={18,57,8,89,7}arrow_forward
- Question 16 What is output? import java.util.ArrayList; public class SimpleCar { @Override public String toString(){ return "I drive fast"; } public static void main(String[] args) { ArrayList myStuff; myStuff = new ArrayList(); myStuff.add(new String("Greetings")); myStuff.add(new Object()); myStuff.add(new SimpleCar()); for(Object item : myStuff) { System.out.println(item.toString()); } String Object SimpleCar java.lang.Object@19cc java.lang.Object@23fb java.lang.Object@ab79 java.lang.String@169b java.lang.Object@23fb java.lang.SimpleCar@a42b Greetings java.lang.Object@169b I drive fast }arrow_forwardjava Create a method that: is called timesTwo returns an ArrayList of Integers takes in a single parameter - an ArrayList of Integers called nums This method should take the ArrayList parameter and multiply every value by two. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(timesTwo(list)); } }arrow_forwardThis codes not complie it can you fix the code can complie it? import java.util.ArrayList; public class Heap { void heapify(ArrayList hT, int i) { int size = hT.size(); int largest = i; int l = 2 * i + 1; int r = 2 * i + 2; if (l < size && hT.get(l) > hT.get(largest)) largest = l; if (r < size && hT.get(r) > hT.get(largest)) largest = r; if (largest != i) { int temp = hT.get(largest); hT.set(largest, hT.get(i)); hT.set(i, temp); heapify(hT, largest); } } void insert(ArrayList hT, int newNum) { int size = hT.size(); if (size == 0) { hT.add(newNum); } else { hT.add(newNum); for (int i = size / 2 - 1; i >= 0; i--) { heapify(hT, i); } } } void deleteNode(ArrayList hT, int num) { int size = hT.size(); int i; for (i = 0; i < size; i++) { if (num == hT.get(i)) break; } int temp = hT.get(i); hT.set(i,…arrow_forward
- Code debug JAVA import java.util.ArrayList;public class ArrayList {public static ArrayList<Integer> reverse(ArrayList<Integer> list) {for (int i = 0; i < list.size(); i++) {System.out.println(list);}public static ArrayList<Integer> getReverse(ArrayList<Integer> list){for (int index = 0; index < list.size() / 2; index++) {int temp = list.get(index);list.set(index, list.get(list.size() - index - 1));//swaplist.set(list.size() - index - 1, temp); //swap}return list;}}public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);System.out.println("Original elements : " + list);System.out.println("Reversed elements : " + getReverse(list));}}arrow_forwardjava Create a static method that: is called appendPosSum returns an ArrayList of Integers takes one parameter: an ArrayList of Integers This method should: Create a new ArrayList of Integers Add only the positive Integers to the new ArrayList Sum the positive Integers in the new ArrayList and add the Sum as the last element For example, if the incoming ArrayList contains the Integers (4,-6,3,-8,0,4,3), the ArrayList that gets returned should be (4,3,4,3,14), with 14 being the sum of (4,3,4,3). The original ArrayList should remain unchanged. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(appendPosSum(list));arrow_forwardNeed help with menu loop pleasearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





