如何找出特定名称重复了多少次?

huangapple go评论82阅读模式
英文:

How to find a particular name repeated how many number of times?

问题

我遇到了输出问题,有人能帮我吗?
这是一个用于查找特定名称重复了多少次的代码。

package example1;
import java.util.Scanner;
public class example1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			int i,j,t,c;
			String a[]=new String[10];
			String b[]=new String[10];
			Scanner sc=new Scanner(System.in);
			for(i=0;i<10;i++)
			{
				a[i]=sc.nextLine();
				b[i]=a[i];
			}
			for(i=0;i<10;i++)
			{
				c=0;
				for(j=i+1;j<9;j++)
				{
					if(b[j]==b[i])
					{
						c++;
						for(t=j;t<10;t++)
						{
							b[t]=b[t+1];
						}
				    }
			 	}
				 System.out.print(b[i]+" 被重复了 "+ c +" 次 ");
			   }
	        }

}
英文:

I'm recieving issue with the output can anyone help me with it?
It is a code for finding how many times a particular name has been repeated.

package example1;
import java.util.Scanner;
public class example1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			int i,j,t,c;
			String a[]=new String[10];
			String b[]=new String[10];
			Scanner sc=new Scanner(System.in);
			for(i=0;i&lt;10;i++)
			{
				a[i]=sc.nextLine();
				b[i]=a[i];
			}
			for(i=0;i&lt;10;i++)
			{
				c=0;
				for(j=i+1;j&lt;9;j++)
				{
					if(b[j]==b[i])
					{
						c++;
						for(t=j;t&lt;10;t++)
						{
							b[t]=b[t+1];
						}
				    }
			 	}
				 System.out.print(b[i]+&quot; is repeated &quot;+ c +&quot; times &quot;);
			   }
	        }

}

答案1

得分: 1

试试这个。

String[] arr = new String[] {"a", "b", "c", "d", "a", "b", "c", "a", "b", "a"};
List<String> list = Arrays.asList(arr);
Set<String> set = new HashSet<>(list);
for (String name : set)
    System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");

如果你希望比较时不区分大小写,可以试试这个。

String[] arr = new String[] {"A", "B", "c", "D", "a", "b", "c", "a", "b", "a"};
List<String> list = new ArrayList<>(10);
for (String name : arr)
    list.add(name.toLowerCase());
Set<String> set = new HashSet<>(list);
for (String name : set)
    System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");

但会需要更多的内存空间。

英文:

Try this.

String[] arr = new String[] {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;};
List&lt;String&gt; list = Arrays.asList(arr);
Set&lt;String&gt; set = new HashSet&lt;&gt;(list); 
for(String name : set)
	System.out.println(name + &quot; is repeated &quot; + Collections.frequency(list, name) + &quot; times&quot;);			

If you want your comparison to be case insensitive, then try this.

String[] arr = new String[] {&quot;A&quot;,&quot;B&quot;,&quot;c&quot;,&quot;D&quot;,&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;};
List&lt;String&gt; list = new ArrayList&lt;&gt;(10);
for(String name : arr)
	list.add(name.toLowerCase());
Set&lt;String&gt; set = new HashSet&lt;&gt;(list); 
for(String name : set)
	System.out.println(name + &quot; is repeated &quot; + Collections.frequency(list, name) + &quot; times&quot;);

Will require more space though.

答案2

得分: 0

以下代码应有助于确定重复项:

public class Repetition {
    public static void main(String[] args) {
        String string = "Sam sam is good boy james is sams friend clyra is clyra";
        int count;
        string = string.toLowerCase();
        String words[] = string.split(" ");

        System.out.println("重复的单词:");
        for(int i = 0; i < words.length; i++) {
            count = 1;
            for(int j = i+1; j < words.length; j++) {
                if(words[i].equals(words[j])) {
                    count++;
                    words[j] = "0"; // 为了以后的参考将其置为0
                }
            }
            if(count > 1 && !words[i].equals("0"))
                System.out.println(words[i]);
        }
    }
}
英文:

The following code should help for determining the repetition

  public class Repetation {  
        public static void main(String[] args) {  
            String string = &quot;Sam sam is good boy james is sams friend clyra is clyra&quot;;  
            int count;   
            string = string.toLowerCase(); 
            String words[] = string.split(&quot; &quot;);  
              
            System.out.println(&quot;Duplicate words: &quot;);   
            for(int i = 0; i &lt; words.length; i++) {  
                count = 1;  
                for(int j = i+1; j &lt; words.length; j++) {  
                    if(words[i].equals(words[j])) {  
                        count++; 
                        words[j] = &quot;0&quot;;//making it 0  for future reference  
                    }  
                }  
                if(count &gt; 1 &amp;&amp; words[i] != &quot;0&quot;)  
                    System.out.println(words[i]);  
            }  
        }  
    }  

答案3

得分: 0

    public static int countWord(String input, String text) {
        int index = input.indexOf(text);
        if (index == -1 || text.isEmpty()) {
            return 0;
        }
        
        int count = 0;
        do {
            count++;
            index = input.indexOf(text, index + text.length());
        } while (index > 0 && index < input.length());
        return count;
    }

This code will return 4:

int total = countWord("abcs8abc88habcabci7h", "abc");
英文:
    public static int countWord(String input, String text) {
        int index = input.indexOf(text);
        if (index == -1 || text.isEmpty()) {
            return 0;
        }
        
        int count = 0;
        do {
            count++;
            index = input.indexOf(text, index + text.length());
        } while (index &gt; 0 &amp;&amp; index &lt; input.length());
        return count;
    }

This code will return 4:

int total = countWord(&quot;abcs8abc88habcabci7h&quot;, &quot;abc&quot;);

huangapple
  • 本文由 发表于 2020年9月11日 12:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定