import java.util.*;
public class DuplicateCharacters {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("Enter String: ");
String str = s.nextLine();
for(int i=0;i<str.length()-1;i++)
{
for(int j=i+1;j<str.length();j++)
{
if(str.charAt(i)==str.charAt(j))
{
System.out.print(str.charAt(j));
}
}
}
}
}
我的代码适用于字符串仅包含2个重复字符..
e.g。假设输入=印度它打印我 但是当输入aaa时,output = a a a ..应该只打印一次
答案 0 :(得分:2)
尝试将每个新读取的字符放入Unresolved external symbol _WinMain@16
。如果添加到集合返回false,则将其添加到跟踪所有重复字符的集合中。这是伪代码:
Set<Character>
答案 1 :(得分:2)
如果可以使用第三方库,则可以使用Eclipse Collections使用以下库:
String str = "india";
MutableCharSet dupes = Strings.asChars(str).toBag().selectDuplicates().toSet();
System.out.println(dupes.makeString("")); // outputs i
Strings
类将从对CharAdapter
的调用中返回asChars
。 toBag
方法将返回一个MutableCharBag
,其中包含char
中的所有String
值及其对应的计数。 selectDuplicates
方法将返回另一个MutableCharBag
,仅包含出现次数不止一次的char
值。 toSet
方法返回一个MutableCharSet
,它将具有重复的唯一char
值。 makeString("")
方法将返回存储在MutableCharSet
中的字符的字符串,它们之间没有分隔符。
注意:我是Eclipse Collections的提交者。
答案 2 :(得分:1)
我首先将问题分解为两部分。首先,您需要一种方法来计算给定起始索引中给定字符的出现次数。这可能看起来像,
static int countFrom(String str, char ch, int start) {
int count = 0;
for (int i = start; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
return count;
}
然后您可以将其与Set<Character>
一起使用(以跟踪您已使用过的字符),例如
Scanner s = new Scanner(System.in);
System.out.println("Enter String: ");
String str = s.nextLine();
Set<Character> set = new HashSet<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!set.contains(ch)) {
int count = countFrom(str, ch, i);
if (count > 1) {
System.out.print(ch);
}
set.add(ch);
}
}
System.out.println();
如果没有Set
,您可以模仿String
之类的
String setString = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!setString.contains(String.valueOf(ch))) {
int count = countFrom(str, ch, i);
if (count > 1) {
System.out.print(ch);
}
setString += ch;
}
}
答案 3 :(得分:0)
这是解决它的另一种方法..
Scanner s = new Scanner(System.in);
System.out.println("Enter String: ");
String str = s.nextLine();
String duplicates = "";
for (int i = 0; i < str.length() - 1; i++) {
for (int j = i + 1; j < str.length(); j++)
{
if (str.charAt(i) == str.charAt(j)) {
if (!duplicates.contains(String.valueOf(str.charAt(j)))) {
duplicates += str.charAt(j);
break;
}
}
}
}
System.out.println(duplicates);
答案 4 :(得分:0)
或考虑一张地图
String word = "aiaiiab";
Map <Character, Integer> charMap = new HashMap<Character, Integer> ();
for (char c : word.toCharArray()) {
Integer count = charMap.get(new Character(c));
if (count == null) {
charMap.put(new Character(c), 1);
}
else {
charMap.put(new Character(c), count.intValue() + 1);
}
}
for (Entry<Character, Integer> obj : charMap.entrySet()) {
if (obj.getValue() > 1) {
System.out.println(obj.getKey());
}
}
答案 5 :(得分:0)
package com.java2novice.algos;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DuplicateCharsInString {
public void findDuplicateChars(String str){
Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
char[] chrs = str.toCharArray();
for(Character ch:chrs){
if(dupMap.containsKey(ch)){
dupMap.put(ch, dupMap.get(ch)+1);
} else {
dupMap.put(ch, 1);
}
}
Set<Character> keys = dupMap.keySet();
for(Character ch:keys){
if(dupMap.get(ch) > 1){
System.out.println(ch+"--->"+dupMap.get(ch));
}
}
}
public static void main(String a[]){
DuplicateCharsInString dcs = new DuplicateCharsInString();
dcs.findDuplicateChars("Java2Novice");
}
}
答案 6 :(得分:0)
public class GFG
{
static final int NO_OF_CHARS = 256;
/* Fills count array with frequency of characters */
static void fillCharCounts(String str, int[] count)
{
for (int i = 0; i < str.length(); i++)
count[str.charAt(i)]++;
}
/* Print duplicates present in the passed string */
static void printDups(String str)
{
// Create an array of size 256 and fill count of every character in it
int count[] = new int[NO_OF_CHARS];
fillCharCounts(str, count);
for (int i = 0; i < NO_OF_CHARS; i++)
if(count[i] > 1)
System.out.printf("%c, count = %d \n", i, count[i]);
}
// Driver Method
public static void main(String[] args)
{
String str = "test string";
printDups(str);
}
}
答案 7 :(得分:0)
import java.util.*;
public class DuplicateCharacters {
static final int NO_OF_CHARS = 256;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter String: ");
String str = s.nextLine();
int count[] = new int[NO_OF_CHARS];
for(int i=0;i<str.length();i++)
{
count[str.charAt(i)]++;
}
for (int i = 0; i < NO_OF_CHARS; i++)
if(count[i] > 1)
System.out.printf("%c", i);
}
}
答案 8 :(得分:0)
转换char数组中的String并将其附加到String builder中,并检查String builder是否已包含该char,然后打印该重复的char。
public static void findduplicate(String word) {
char arr[] = word.toCharArray();
StringBuilder s = new StringBuilder();
for (char c : arr) {
int index = s.indexOf("" + c);
if (index != -1) {
System.out.println("duplicate character "+c);
} else {
s.append(c);
}
}
}