Java Maps and Hashmaps
Anonymous
import java.util.*; public class GetDuplicates { public static void main(String args[]){ // get input Scanner sc = new Scanner(System.in); ArrayList nums = new ArrayList(); while(sc.hasNext()) { String num = sc.next().trim(); if (num.equals("q")) break; nums.add(Integer.parseInt(num)); } Set set = new HashSet(); for(int n : nums) { if(set.contains(n)) { System.out.print(n + " "); } else { set.add(n); } } } }
Check out your Company Bowl for anonymous work chats.