Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class MyCode {
- /**
- * 1
- * Unique
- *
- * Given an array of integers, return an array with all duplicates removed.*
- *
- * Parameters
- * Input: arr {Array of Integers}
- * Output: {ArrayList of Integers}
- *
- * Constraints
- *
- * Time: O(N)
- * Space: O(N)
- *
- * Examples
- * [1, 2, 4, 4, 5, 6] --> [1, 2, 4, 5, 6]
- * [1, 1, 2, 2, 3, 3]' --> [1, 2, 3]
- * [1, 2, 3, 1, 2] --> [1, 2, 3]
- */
- public static void main (String[] args) {
- int[]arr = new int[]{1, 2, 3, 1, 2};
- //ArrayList<Integer> resultsList = new ArrayList<Integer>();
- //resultsList = unique(arr);
- System.out.println(unique(arr));
- }
- public static ArrayList<Integer> unique(int[] arr) {
- HashSet<Integer> set = new HashSet<Integer>();
- for(int i = 0; i<arr.length; i++){
- set.add(arr[i]);
- }
- ArrayList<Integer> list = new ArrayList<Integer>(set);
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement