-1

Without using a data structure, I need to display whether each character in a string is unique or not.

I wrote the code and it works for paa but it doesn't work for pak.

var String = "paa"
//var String = "pak"
var splittedString = String.split();

for(i = 0; i < splittedString.length; i++) {

    if(splittedString[i] === splittedString[i+ 1] || 
            splittedString[i+1] === splittedString[i + 2]) {
        console.log("not unique string");
    } else {
        console.log("its an unique string")
    }
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
  • Your code uses a data structure, I'm sorry to say. What is the point of that restriction? – Pointy Apr 21 '17 at 16:11
  • There is a lot of questions about it. For example: http://stackoverflow.com/questions/12870489/regex-to-match-a-word-with-unique-non-repeating-characters – dagatsoin Apr 21 '17 at 16:11
  • 1
    Possible duplicate of [Javascript: Determine if all characters in a string are unique and if not, delete duplicate characters](http://stackoverflow.com/questions/17285948/javascript-determine-if-all-characters-in-a-string-are-unique-and-if-not-delet) – Mayank Shukla Apr 21 '17 at 16:12
  • 2
    Why is this tagged `reactjs`, `jquery`, `angular`, and `backbone.js`? It has nothing to do with those libraries / frameworks. – Todd Chaffee Apr 21 '17 at 16:18

3 Answers3

1

It is clearly stated in the problem don't need to use the data structure. I saw above answers using an array. I try to solve this issue in C#. Let me know if you have any feedback.

public bool IsUnique(string text)
{
    if (text.Length > 256) return false;

    for (var indx = 0; indx < text.Length; indx++)
    {
        for (var jndx = indx + 1; jndx < text.Length; jndx++)
        {
            // compare character 
            if (text.Substring(indx, 1) == text.Substring(jndx, 1) )
            {
                return false;
            }
        }
    }

    return true;
}
0

First you sort the characters in the string and then compare each one to the next.
The program with regex may look like below

var str = "alphabet";
var sstr = str.split('').sort().join('');
var result = /(.)\1/.test(sstr);
console.log(sstr + " has dups: " + result)
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
0

Just to show the needed change in your code, plus nice output as per Emile's request:

var str = "palace";
//var str = "pak";
var splittedString = str.split('');

for (i=0; i<splittedString.length; i++) {
  var c = splittedString[i];
  var unique = true;
  for (j=0; j <splittedString.length; j++) {
    if (i==j) continue;
    if (c === splittedString[j]) {
      unique = false;
      break;
    }
  }
  console.log("'" + c + "' is" + (unique ? '': ' not') + " unique character");
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • var String = "paa" //var String = "pak" var splittedString = String.split().sort(); for(i=0; i –  Apr 21 '17 at 18:28
  • hey I used your logic, but still not working...can you tell me why its not working... –  Apr 21 '17 at 18:29
  • updated but still doesn't work ---> var String = "paa" //var String = "pak" var splittedString = String.split() console.log("outside outer loop"); for(i=0; i –  Apr 21 '17 at 18:49
  • 1
    Your split method should define some character separator – MaxZoom Apr 21 '17 at 19:21
  • still not working ---> var wholeWord = "paa"; //var String = "pak" var splittedString = wholeWord.split(''); for(i=0; i –  Apr 21 '17 at 19:50
  • hey thanks for your help I got it working https://jsfiddle.net/at661zsu/ –  Apr 21 '17 at 20:10
  • have one more doubt...is it possible to achieve without using data structure since split will return in array right...since array is a data structure right?? –  Apr 21 '17 at 20:22
  • 1
    Yes, glad you have it resolved. Please up-vote the answer(s) (if applicable) – MaxZoom Apr 21 '17 at 20:38
  • 1
    You can try [String.charAt(..)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt) instead of array`[index]` notation. – MaxZoom Apr 21 '17 at 21:16
  • I tried using charAt but its not working as expected, can you let me know what is the problem https://jsfiddle.net/at661zsu/1/ –  Apr 22 '17 at 00:34
  • hello can you help me with this one http://stackoverflow.com/questions/43600110/minimise-the-code-since-i-am-using-the-same-code-only-the-content-in-p-tags-chan –  Apr 25 '17 at 03:35