在正则表达式中查找重复词的Java程序
给定一个由字符串表示的表达式。任务是在Java的正则表达式中查找重复元素。使用映射或集合数据结构来识别句子中单词的唯一性。
例子:
Input : str = " Hi, I am Hritik and I am a programmer. "
Output: I am
Explanation: We are printing duplicate words in the given Expression.
Input : str = " Ironman is alive. "
Output: There is no Duplicate Word.
Explanation: There are no duplicate words present in the given Expression.
方法一:
- 获取表达式。
- 将所有单词存储在一个数组中。
- 使用正则表达式 '\\W' 拆分单词。 (正则表达式的使用)
- 在数组中迭代并在 Map 中存储单词和所有出现次数。
- 现在,在地图中,如果出现次数大于 1,则我们正在打印单词。
下面是上述方法的实现:
Java
// Find Duplicate Words in a Regular Expression in Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// we have a expression
String expression
= "Hi, I am Hritik and I am a programmer";
// splitting words using regex
String[] words = expression.split("\\W");
// we are creating a Map for storing
// strings and it's occurrence"
Map word_map = new HashMap<>();
// Here we are iterating in words array and
// increasing it's occurrence by 1.
for (String word : words) {
if (word_map.get(word) != null) {
word_map.put(word, word_map.get(word) + 1);
}
// if the word came once then occurrence is 1.
else {
word_map.put(word, 1);
}
}
// creating a keyset of word_map
Set word_set = word_map.keySet();
// We are iterating in word set
for (String word : word_set) {
// if word matched then checking occurrence
if (word_map.get(word) > 1)
// here we are printing the duplicate words
System.out.println(word);
}
}
}
Java
// Find Duplicate Words in a Regular Expression in Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
String expression
= "Hi, I am Hritik and I am a programmer";
// splitting words using regex
String[] words = expression.split("\\W");
// creating object of HashSet class implemented by
Set set = new HashSet<>();
// here we are iterating in Array
for (int i = 0; i < words.length - 1; i++) {
for (int j = 1; j < words.length; j++) {
// if strings matched then adding strings in
// Set because if we ad same string set will
// remove one and we have only repeated
// words.
if (words[i].equals(words[j]) && i != j) {
set.add(words[i]);
}
}
}
// here we are printing the set
System.out.println(set);
}
}
输出
I
am
方法二:
- 获取表达式。
- 将所有单词存储在一个数组中。
- 使用正则表达式 '\\W' 拆分单词。 (正则表达式的使用)
- 通过迭代将数组的每个单词与其他单词匹配。
- 如果单词匹配,则在 Set 中添加单词,因为 set 删除了迭代流程添加的重复单词。
- 最后,我们正在打印该集合。
下面是上述方法的实现:
Java
// Find Duplicate Words in a Regular Expression in Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
String expression
= "Hi, I am Hritik and I am a programmer";
// splitting words using regex
String[] words = expression.split("\\W");
// creating object of HashSet class implemented by
Set set = new HashSet<>();
// here we are iterating in Array
for (int i = 0; i < words.length - 1; i++) {
for (int j = 1; j < words.length; j++) {
// if strings matched then adding strings in
// Set because if we ad same string set will
// remove one and we have only repeated
// words.
if (words[i].equals(words[j]) && i != j) {
set.add(words[i]);
}
}
}
// here we are printing the set
System.out.println(set);
}
}
输出
[I, am]