import java.util.*;
public class DuplicateCharacters {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("String: ");
String str = sc.nextLine();
sc.close();
Map<Character, Integer> counts = new HashMap<>();
boolean hasDupes = false;
for(char c : str.toCharArray()) {
if(counts.containsKey(c)) {
hasDupes = true;
counts.put(c, counts.get(c) + 1);
}
else {
counts.put(c, 1);
}
}
if(hasDupes) {
long numDupes = counts.values().stream().filter(v -> v > 1).count();
System.out.printf("======%d DUPLICATES FOUND!=======\n", numDupes);
for(char c : counts.keySet()) {
if(counts.get(c) > 1) {
System.out.printf("\"%c\" - %d\n", c, counts.get(c));
}
}
for(int i = 0, additional = String.valueOf(numDupes).length(); i < 31+additional; i++) {
System.out.print("=");
}
System.out.println();
}
else {
System.out.println("No duplicates found");
}
}
}
import java.util.ArrayList;
import java.util.Optional;
class Entry {
String character;
int count;
Entry(String character, int count) {
this.character = character;
this.count = count;
}
}
public class Main {
public static void main(String[] args) {
printDuplicates("Pickles are delicious mate holy moly!");
printDuplicates("Yo boy, what's up?");
}
public static void printDuplicates(String text) {
ArrayList<Entry> duplicates = findDuplicates(text);
System.out.println("======" + duplicates.size() + " DUPLICATES FOUND!=======");
for (Entry duplicate: duplicates) {
System.out.println("\"" + duplicate.character + "\" - " + duplicate.count);
}
System.out.println("===============================");
System.out.println();
}
public static ArrayList<Entry> findDuplicates(String text) {
ArrayList<Entry> entries = new ArrayList<>();
ArrayList<Entry> duplicates = new ArrayList<>();
for (String c: text.split("")) {
if (entries.stream().anyMatch(x -> x.character.equals(c))) {
Optional<Entry> tmp = entries.stream().filter(x -> x.character.equals(c)).findFirst();
if (tmp.isPresent()) {
Entry entry = tmp.get();
entry.count++;
}
} else {
entries.add(new Entry(c, 1));
}
}
for (Entry entry : entries) {
if (entry.count <= 1) continue;
duplicates.add(entry);
}
return duplicates;
}
}
import java.util.HashMap;
import java.util.Scanner;
public class Duplicate {
public static void main(String[] args) {
HashMap<Character, Integer> charInt = new HashMap<>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string of text: ");
String text = scanner.nextLine();
for (char letter : text.toCharArray()) {
if (!charInt.containsKey(letter)){
charInt.put(letter, 1);
} else {
int repetitions = charInt.get(letter);
charInt.put(letter, repetitions + 1);
}
}
HashMap<Character, Integer> repeated = new HashMap<>();
for (int i = 0; i < charInt.size(); i++){
int number = (int) charInt.values().toArray()[i];
if(number > 1) {
char added = (char) charInt.keySet().toArray()[i];
repeated.put(added, number);
}
}
System.out.printf("There were %d repeated characters: \n", repeated.size());
for (int i = 0; i < repeated.size(); i++){
int number = (int) repeated.values().toArray()[i];
char letter = (char) repeated.keySet().toArray()[i];
System.out.printf("\"%s\" - %d %n", letter, number);
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
HashMap<Character, Integer> map = new HashMap<>();
int c = 0;
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
for (char ch : input.toCharArray()){
if (!map.containsKey(ch)) map.put(ch, 1);
else {
map.put(ch, map.get(ch) + 1);
if (map.get(ch) == 2) c++;
}
}
System.out.printf("======%d DUPLICATES FOUND!=======\n", c);
ArrayList<Map.Entry<Character, Integer>> ar = new ArrayList<>(map.entrySet());
Comparator<Map.Entry<Character, Integer>> comp = (o1, o2) -> o1.getValue() == o2.getValue()?Character.compare(o1.getKey(), o2.getKey()):Integer.compare(o1.getValue(), o2.getValue());
ar.sort(comp.reversed());
for (Map.Entry<Character, Integer> M : ar){
if (M.getValue() == 1) continue;
System.out.printf("\"%c\" - %d\n", M.getKey(), M.getValue());
}
System.out.println("===============================");
}
}
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class Main {
private static Map<String, Integer> stringMap = new HashMap<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = in.nextLine();
// loop through all characters in the
for(String c : input.split("")) {
if(!stringMap.containsKey(c)) {
stringMap.put(c, 1);
} else {
stringMap.put(c, stringMap.get(c) + 1);
}
}
boolean foundDupes = stringMap.values().stream().anyMatch(val -> val > 1); // find dupes
// print dupes if there are
if(foundDupes) {
System.out.println("Found dupes!");
stringMap.forEach((s, count) -> {
if(count > 1)
System.out.println("Character: " + s + ", Count: " + count);
});
} else {
System.out.println("No dupes found!");
}
}
}
package me.kodysimpson;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static HashMap<Character, Integer>countCharacters(String thing){
HashMap<Character, Integer> char_count = new HashMap<>();
//Loop through the entire String, noting chars
for (int i = 0; i < thing.length(); i++){
//See if the char is on the hashmap already
if (char_count.containsKey(thing.charAt(i))){ //already in our hashmap, so just increment the current count
char_count.replace(thing.charAt(i), char_count.get(thing.charAt(i)) + 1);
}else{ //the char is not in the list already, so add it
char_count.put(thing.charAt(i), 1);
}
}
return char_count;
}
public static void reportDuplicates(HashMap<Character, Integer> chars_count){
//Let's cycle through the map first to see how many total duplicates we want to announce there are
AtomicInteger dup_count = new AtomicInteger(); //lambdas don't let you use outside variables(unless final) so intellij let me do this
chars_count.forEach((character, count) -> {
if (count > 1){
dup_count.getAndIncrement();
}
});
System.out.println("====DUPLICATES FOUND: " + dup_count + " ====");
//Now, let's loop one final time to show all of the duplicates
chars_count.forEach((character, count) -> {
if (count > 1){
System.out.println("'" + character + "' : count: " + count);
}
});
//The reason we use the forEach with lambda is because there
// is not otherwise a simple way of getting both the key and value.
// This does it for you
}
public static void main(String[] args) {
//Our test String
String thing = "Riley is two feet tall and that is the gospel truth.";
HashMap<Character, Integer> chars_count = countCharacters(thing);
reportDuplicates(chars_count);
}
}