package com.company;
import java.util.HashSet;
import java.util.Set;
public class Algorithm {
Set<Character> duplicate(String string) {
if (string.isBlank() || string.isEmpty()) {
throw new RuntimeException("Invalid String");
}
Set<Character> characters = new HashSet<>();
char[] chars = string.toCharArray();
for (char aChar : chars) {
if (string.indexOf(aChar) != string.lastIndexOf(aChar) && !characters.contains(aChar)) {
characters.add(aChar);
}
}
return characters;
}
}
package com.company;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AlgorithmTest {
@Test
void duplicate1() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("aabbc");
Assertions.assertEquals(new HashSet<>(Arrays.asList('a', 'b')), duplicate);
}
@Test
void duplicate2() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("sshhhrriiikkaaannttt");
Assertions.assertEquals(new HashSet<>(Arrays.asList('s', 'h', 'r', 'i', 'k', 'a', 'n', 't')), duplicate);
}
@Test
void duplicate3() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("madam");
Assertions.assertEquals(new HashSet<>(Arrays.asList('m', 'a')), duplicate);
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
useJUnitPlatform()
}
package com.company;
import java.util.HashSet;
import java.util.Set;
public class Algorithm {
Set<Character> duplicate(String string) {
if (string.isBlank() || string.isEmpty()) {
throw new RuntimeException("Invalid String");
}
Set<Character> characters = new HashSet<>();
char[] chars = string.toCharArray();
for (char aChar : chars) {
if (string.replaceFirst(String.valueOf(aChar), "").length() != string.replaceAll(String.valueOf(aChar), "").length() && !characters.contains(aChar)) {
characters.add(aChar);
}
}
return characters;
}
}
package com.company;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AlgorithmTest {
@Test
void duplicate1() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("aabbc");
Assertions.assertEquals(new HashSet<>(Arrays.asList('a', 'b')), duplicate);
}
@Test
void duplicate2() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("sshhhrriiikkaaannttt");
Assertions.assertEquals(new HashSet<>(Arrays.asList('s', 'h', 'r', 'i', 'k', 'a', 'n', 't')), duplicate);
}
@Test
void duplicate3() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("madam");
Assertions.assertEquals(new HashSet<>(Arrays.asList('m', 'a')), duplicate);
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
useJUnitPlatform()
}
package com.company;
import java.util.HashSet;
import java.util.Set;
public class Algorithm {
Set<Character> duplicate(String string) {
if (string.isBlank() || string.isEmpty()) {
throw new RuntimeException("Invalid String");
}
Set<Character> characters = new HashSet<>();
char[] chars = string.toCharArray();
for (char aChar : chars) {
if (string.length() - 1 > string.replace(String.valueOf(aChar), "").length() & !characters.contains(aChar)) {
characters.add(aChar);
}
}
return characters;
}
}
package com.company;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AlgorithmTest {
@Test
void duplicate1() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("aabbc");
Assertions.assertEquals(new HashSet<>(Arrays.asList('a', 'b')), duplicate);
}
@Test
void duplicate2() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("sshhhrriiikkaaannttt");
Assertions.assertEquals(new HashSet<>(Arrays.asList('s', 'h', 'r', 'i', 'k', 'a', 'n', 't')), duplicate);
}
@Test
void duplicate3() {
Algorithm algorithm = new Algorithm();
Set<Character> duplicate = algorithm.duplicate("madam");
Assertions.assertEquals(new HashSet<>(Arrays.asList('m', 'a')), duplicate);
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
useJUnitPlatform()
}