Skip to content

Instantly share code, notes, and snippets.

@gotoark
Created May 14, 2019 01:45
Show Gist options
  • Save gotoark/c63ae0a27c9dac4d91a7cc1288eed110 to your computer and use it in GitHub Desktop.
Save gotoark/c63ae0a27c9dac4d91a7cc1288eed110 to your computer and use it in GitHub Desktop.
Find the no of Occurrences in a List without using a Inner Loop
package interviewprograms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class FindTheDuplicates {
public static void main(String args[]) {
List<String> cityList=new ArrayList<>();
cityList.add("Chennai");
cityList.add("Mumbai");
cityList.add("Chennai");
cityList.add("Kolkatta");
cityList.add("Madurai");
cityList.add("Chennai");
cityList.add("Mumbai");
Set<String> citySet=new HashSet<>(cityList);
Map<String,Integer> cityMap=new HashMap<>();
for(String city:citySet) {
cityMap.put(city, 0);
}
for(String city:cityList) {
cityMap.put(city,cityMap.get(city)+1);
}
System.out.println(cityMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment