Created
May 14, 2019 01:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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